How to stop a b2Body from moving once it has reached a point

Hi all,

I have been searching on the internet for a while now for a way to stop a b2Body from moving once it has reached a given position and can't find any proper solutions for my situation. Let me explain what I have. I am working on a top down game for the Iphone, so gravity is 0. I have a player, which I want to move to a touched location on the screen. I have read online that its possible by manually moving the b2Body's transform, but as stated in the documentation "Manipulating a body's transform may cause non-physical behavior", so I don't want to go that route. Another way I read online was by using a b2MouseJoint, but I read that it may cause non-physical behavior as well, and since I want more than just my player to move in the game eventually, I wouldn't want to make a b2MouseJoint for each entity in the game to move to a designated location.

What I am doing currently to move my character is setting its Linear Velocity based on the touch location, but the player will continue to move in this direction forever unless stopped. I know that I can set the players Linear Velocity to 0 once I reach the desired point, so how can I tell that the player, or AI in the future, has reached their stopping position in order to set their velocity to 0? Or is there another way to do this?

Thanks,
SetLinearVelocity sounds like the right approach, and yeah, you'll need to figure a few things out. In this case, can't you just stop the player when he is within some distance of the destination?
Hey pTymN,
Previously, I tried to stop the player once his position was equal to the position I wanted him to move to. This didn't work because of floating point issues, and even casting everything to an int only proved to work 30% of the time, where the other 70%, the player just moved on past that point. If I knew of a proper algorithm in which I could check a general area, possibly entity's body size, whose center is at the desired spot and determine how close to the center of that area I was, it might work. I just worry about being off enough to where it may frustrate the user. Do you know how I might implement such a check?
you could use a sensor to detect when the hero is near the point.

also the reason your hero is moving past the point is probably due to his movement speed.
if your hero is at x=20 and moving at a speed of 7, he will go to x=27 before ever being at x==25.

if you use box2d collision it will be comparing more than just x, it will be like comparing the left and right side of the hero to the left and right side of the sensor, so it will avoid any float point and other issues you seem to be having.

if you have a really high speed hero,
you should either make your sensor big enough to account for the largest possible speed through that area, or set your heros body to isBullet=true, so that it draws a smeared shape for collision detection and will detect collisions between position a and b. (isBullet is slower to calculate though)

also, you could stop the users control and "tween" them to the middle of the sensor when the beginContact is detected. so that you don't have to worry about how far off they are.
you don't need to use an actual tween, you can apply forces in box2d.

it is possible to use an actual tween (example below) but its probably easier to apply forces in box2d for what you need in your game.
here is an example where i switch controls between caurina tweener and box2d physics.
http://www.sketchbookgames.com/Portfolio/
*if you grab one of the squares, box2d takes over. when you release caurina tweener puts them back.
Thats a slick website.
Thanks for the reply sketchbookgames,

I am going to start looking into sensors as a solution. I have one other question about using sensors or stopping my objects in general. I see how a sensor can work for the entities I move in my game by giving them a position to move to, but what about objects I collide with that are dynamic. For instance, I have a box I need to push, the hero bumps up next to it and box2d applies a velocity to the box. Wouldn't the box just slide on forever? How would a sensor solve this issue since I technically wouldn't know the position to place the sensor?

Thanks,
something like this:

Code:
b2Vec2 movement = newPos - oldPos;

bool crossedPoint = false;
if((b2Dot(newPos - thePoint, movement) > 0.0) !=
    (b2Dot(oldPos - thePoint, movement) > 0.0)) {
    crossedPoint = true;
}


There's really no need for a sensor or anything complicated here. You are moving towards the point each turn, correct?
Hey pTymN,

That actually works pretty well. Thanks! Now lastly, how would I stop the object that I push since it doesn't have a destination point? I was thinking I could try and calculate there destination (d = v*t) based off its current velocity and run the same check as you posted, but not sure if that would work, and I'm not sure what time would equal at this point.

Thanks,
Quote:
pTymN: Thats a slick website.

Thank you.

Original thread: https://box2d.org/forum/viewtopic.php?t=6409