box2d and constant movement
Posted
by
Arnas
on Game Development
See other posts from Game Development
or by Arnas
Published on 2012-02-24T11:46:18Z
Indexed on
2012/03/25
23:42 UTC
Read the original article
Hit count: 220
i'm developing a game with a top down view, the players body is a circle. To move the character you need to tap on the screen and it moves to the spot.
To achieve this i'm saving the coordinate of the touch and call a method every frame which applies linear velocity to the body with a vector of the direction the body should go
_body->SetLinearVelocity(b2Vec2((a.x - currPos.x)/SPEED_RATIO,(size.height - a.y - currPos.y)/SPEED_RATIO));
//click position - current position, screen height - click position (since the y axis is flipped, (0,0) is in the bottom left ) - current position = vector of the direction we want to go
now the problem with this is that the body slows down until it finally stops when getting closer to the point we want it to go, since the closer we are to that point the lenght of the vector gets smaller. Besides that i've read that it's bad practice to set linear velocity in box2d and i should use apply force instead, but that way the forces would add up and overshoot the target where it's supposed to stop.
So what i'm asking is how to move a box2d body to a coordinate in constant speed.
© Game Development or respective owner