i have a question about a body, which follows a specific path. First of all here the method to move the body to a target point:
const float destinationControl = 0.3f;
b2Vec2 targetPosition = path[counter];
b2Vec2 missilePosition = _physicalBody->GetPosition();
b2Vec2 diff = targetPosition - missilePosition;
float dist = diff.Length();
if (dist > 0)
{
if(dist < destinationControl){
++counter;
return;
}
// compute the aiming direction
b2Vec2 direction = b2Vec2(diff.x / dist, diff.y / dist);
// get the current missile velocity because we will apply a force to compensate this.
b2Vec2 currentVelocity = _physicalBody->GetLinearVelocity();
// the missile ideal velocity is the direction to the target multiplied by the max speed
b2Vec2 desireVelocity = b2Vec2(direction.x * maxSpeed, direction.y * maxSpeed);
// compensate the current missile velocity by the desired velocity, based on the control factor
b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);
// transform our velocity into an impulse (get rid of the time and mass factor)
float temp = (_physicalBody->GetMass() / normalDelta);
b2Vec2 finalForce = b2Vec2(finalVelocity.x * temp, finalVelocity.y * temp);
_physicalBody->ApplyForce(finalForce, _physicalBody->GetWorldCenter());
}
The array path
represent as the name implies a specific path which the body follow when the user trigger a touch event.
My problem is that the variable maxSpeed is not constant. So when the path is very short and the maxSpeed
is very high the body moves over the current target point. With my method above the body return to the current target point and does not focus the next point.
At the moment i have a constant destinationControl
which controls if the distance between the body and the current target point is short enough to focus the next target point.
The focus is to increase a counter which represent the index of the target points.
So my final question is: Can you imagine another possible solution, because mine is not very safe. Is there a way to figure out if the body moves over the current target point?
thank you in advance and sorry for my bad english.
I had a similar problem in a path following demonstration I was putting together(here is the project and here is the just the video).
I found the most workable solution was to use a combination of 2 functions. The first detects if the entity is "close" to the next point they want to get to.
This entity is using a "seek" operation to move from point to point. If it is close to the point, the function returns true. It uses the square of the distance to avoid square roots for length.
The second function is the path following function executed every update:
This one is a bit harrier, but what it comes down to is checking for whether the entity has reached the final target (_navigatePos), reached the next path point, or unable to approach the target point for some reason, so it times out and replans the path to it.
This is part of a simulation of a ship flying through a moving asteroid field, so the scenery was changing and the ship could get "stuck" in certain situations, so it had to replan every now and then.
The code base for this is located on github.
Was this helpful?
What I think you might want to do is to compare your maxSpeed to the distance possible to move. When moving farther than the distance available, set the misslePosition to the targetPosition and retrieve the next target position in the path, and subtract the distance already moved from maxSpeed.
Another possible solution would be to make smaller steps and perform a loop while totalDistanceCovered < maxSpeed, this might be a little easier to capture more edge cases, although more CPU time is used.
--- My original answer below ---
You may need to set the definition of the _physicalBody to be a bullet if you are attempting to collide with a dynamic target.
From box2d.org/manual