Search code examples
vectorlualove2d

Making a point follow another point


I am writing a very quick mini game, and I don't have any vector implementation for it. This is what I have:

local qx = manx
        local qy = many
        local px = bigenemyx
        local py = bigenemyy
        local vx = qx - px
        local vy = qy - py
        local mag = math.sqrt(vx^2 + vy^2)
        bigenemyx = bigenemyx + mag * speed * dt
        bigenemyy = bigenemyy + mag * speed * dt

bigenemy needs to follow man. bigenemy just disappears off the screen. I've rushed the code for this and am (evidently) not experienced with the maths in this, thanks for any help - apologies if I just haven't thought straight, I haven't really stuck to "more speed less haste"!


Solution

  • Well, you are using 'mag' value in all directions - it is not good. Try

        if(mag < 0.0001): mag = 1
        bigenemyx = bigenemyx + (vx/mag) * speed * dt
        bigenemyy = bigenemyy + (vy/mag) * speed * dt