Game creation - linear movement between two objects (VB.NET / any programming language)

I am working on a small game project and I am facing a problem while trying to move an object from point A to point B with solid motion speed and linear motion.

In my research, I found a code snippet for this, but it doesn't seem to work great as the speed seems to be different depending on the angle it moves to.

This is the code at the moment:

    Dim x As Single, y As Single
    Dim sngInc As Single
    Dim lStepValue As Long

    Dim PT1X = pObj.Left
    Dim PT1Y = pObj.Top
    Dim PT2X = pTarget.Left
    Dim PT2Y = pTarget.Top
    If PT1Y = PT2Y Then
        sngInc = 1
        If PT2X > PT1X Then
            lStepValue = 1
            lStepValue = -1
        End If
        y = PT1Y
        x = PT1X
        x += lStepValue * sngInc
        pObj.Left = x
        pObj.Top = y
        If PT2Y > PT1Y Then
            lStepValue = 1
            lStepValue = -1
        End If
        sngInc = (PT2X - PT1X) / Math.Abs(PT1Y - PT2Y)

        x = PT1X
        y = PT1Y
        y += lStepValue
        x = x + sngInc * Math.Abs(lStepValue)
        pObj.Left = x
        pObj.Top = y
    End If


In my test project, it is under a timer with a 30ms mark. I translated it from the VB6 code I found somewhere. The problem with what I said above is that the movement speed will not be exactly 1, but something near it depending on the angle.

Can anyone provide me with a fix or example in any other programming language how to do this, because I couldn't find the correct help from google, expecting the VB6 code I found and I have no experience on the matter. I'm not even sure if this is called linear motion between two objects.

Thank you so much:)


source to share

1 answer

Your code is a variant of the digital differential analyzer that is used to rasterize the line. But that's not what you want. Here's an alternative:

Given the starting point (PT1X, PT1Y)

and target point (PT2X, PT2Y)

, you first need to initialize the direction of travel. It seems that you are not familiar with vector math, so I am trying to explain it at a low level. But I suggest you take a peek into vector arithmetic sooner or later. It makes life easier.

The direction of movement is simply the difference between two points:

Dim dirX As Single = PT2X - PT1X
Dim dirY As Single = PT2Y - PT1Y


We can calculate the length of this direction (distance of two points) with the Pythagorean theorem:

Dim l = Math.Sqrt(dirX * dirX + dirY * dirY)


If we divide the direction components by this length, we get a direction pointing to the target and having a length of 1. Ie if we add this direction to a point, the point moves exactly one unit towards the target. But we probably want to indicate a certain speed v

. Therefore, we multiply by this:

dirX = dirX * v / l
dirY = dirY * v / l


All of the above steps can be performed before any movement. Now updating in every frame is simple:

pObj.Left += dirX
pObj.Top += dirY




All Articles