Predicting the linearVelocity to move an object

Hello guys I’m thinking of a type of enemy to add to my game, thankfully I know exactly what I’m looking for and I have everything else apart from this. The way the enemy moves is by having one ray cast rotating round the center of the enemy looking for ground or walls, as long as where the ray cast point hits is over a distance of says 100-200 pixels then jump to that point. What I want to figure out, is how to calculate (predict) the linear velocity that body needs to land at that position in a parabolic curve. As long as I can do that then everything is sound, so any suggestions please shoot. The enemy will move around sticking to walls, floors, ceilings, until the player comes in to view, then he will (upon viewing the player) change the ray cast to only rotate in that direction and plot points towards the player. When in sufficient distance the enemy plunges himself towards the players head to knock him down, depending on the severity of the collision the player will either find his feet (they get ‘unjointed’ from the player) using the same parabolic hopping method, or die.

Anyone feel free to take this idea of the enemy if you know the solution or the solution has been found.

@Luatee Do a google search for trajectory equation. That will give you several formulas you can use.

@dave1707 Thanks thats the word I was looking for as well. I’ll have a look. Although you understand I’m looking to find the velocity I need to make the body jump to that position, with a variable height.

@Luatee Are you going to vary the trajectory angle. A trajectory angle of 45 degrees will give you the max distance at a fixed velocity.

@Luatee If you want a trajectory of 45 degrees, then the velocity is math.sqrt(160*distance).
So the velocity of the object at 45 degrees will be linearVelocity=vec2(velocity,velocity). If you want different trajectory angles, then the 2 vec2 velocities will have to be changed for the angle you want.

@Luatee Here’s an example of a trajectory at 45 degrees. Slide you finger right or left to change the distance of the target. The velocity will change accordingly.

EDIT: Changed g (gravity) from 160 to 158.


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    g=158
    dist=500

    v=math.sqrt(g*dist)      -- velocity calculation

    b=physics.body(CIRCLE,5)
    b.x=0
    b.y=100   
    b.linearVelocity=vec2(v,v)
    b.interpolate=true
end

function draw()
    background(40,40,50)
    fill(255)
    text("Distance  "..dist,WIDTH/2,HEIGHT-50)
    text("Velocity  "..v,WIDTH/2,HEIGHT-100)
    text("Slide your finger right or left to change the distance and velocity",WIDTH/2,HEIGHT-150)
    stroke(255)
    strokeWidth(2)
    line(0,100,WIDTH,100)
    ellipse(dist,100,10)
    ellipse(0,100,10)
    ellipse(b.x,b.y,10)   
end

function touched(t)
    if t.state==MOVING then
        dist=dist+t.deltaX
        v=math.sqrt(g*dist)
    end
    if t.state==ENDED then
        b=physics.body(CIRCLE,5)
        b.x=0
        b.y=100   
        b.linearVelocity=vec2(v,v)
        b.interpolate=true
        b.linearVelocity=vec2(v,v)        
    end
end

@dave1707 No need for that, I wanted varying angles at a set height, I’ve eventually found the equation I was looking for! Thanks for the help though, you’re always firing solutions at everything.