This is my libgdx code for finding the linear velocity needed for a box2d body to reach…
- User
This is my libgdx code for finding the linear velocity needed for a box2d body to reach a position. It uses an interpolation function to do sort of arrival behavior where the velocity slows down as it approaches the position to avoid overshooting.
However, some interpolation functions do worse with higher velocities and still overshoot. Can you add a check at the end to find if the velocity is higher than needed to reach the position. And limit the velocity as so
To clarify, the maxSpeed param is not the speed needed to reach the position, it's the maximum speed velocity is allowed to be at any time. maxSpeed may very well be higher than needed to reach the target position.
You need to check if say applying 5 meters per second velocity to cross .1 meters distance will overshoot the next frame. And limit the velocity to just what is needed to cross .1 meters
java/** * @param maxSpeed the maximum speed in meters per second */ public static Vector2 velocityToPosition(Body body, Vector2 targetPosition, float maxSpeed) { Vector2 position = body.getPosition(); Vector2 velocity = body.getLinearVelocity().cpy(); Vector2 targetDirection = targetPosition.cpy().sub(position); float distance = targetDirection.len(); //distance that can be covered in one time step float maxDistancePerStep = maxSpeed / PhysicsSystem.STEP_FREQUENCY; if (distance <= maxDistancePerStep) { velocity.setZero(); } else { //find the velocity needed to reach the target position float targetSpeed = Math.min(maxSpeed, distance / maxDistancePerStep); Vector2 targetVelocity = targetDirection.cpy().nor().scl(targetSpeed); float distanceToTarget = targetPosition.dst2(position); if (distanceToTarget < 0.01f) { velocity.setZero(); } else { //reduce the velocity as the body approaches the target position //avoids overshooting position float factor = Math.min(1, distanceToTarget / (maxDistancePerStep * maxDistancePerStep)); velocity.interpolate(targetVelocity, factor, Interpolation.exp5Out); } } return velocity; } - Assistant
You can add a check at the end of the function to limit the velocity based on the distance to the target position, as you described. Here's the modified code:
java - User
I'm using libgdx with box2d. Write me a util method that calculates the velocity needed to push a box2d body to a target position like
javaVector2 velocity = PhysicsUtils.velocityToPosition(body, target, maxSpeed); body.setLinearVelocity(velocity);I want the velocity to slow down gradually in relation to the distance between current body position and target. Velocity should slow down more as the position gets closer to target. I want to control this with an Interpolation function.
This is to avoid overshooting the position. The returned velocity should never be greater than needed to reach the target position in the next frame.
This method should be able to smoothly push a body with it's returned velocity even when the distance to target is very small like 0.01f
- Assistant
- User
I’m using libgdx with box2d. Write me a util method that calculates the velocity needed to push a box2d body to a target position like
codeVector2 velocity = PhysicsUtils.velocityToPosition(body, target, maxSpeed); body.setLinearVelocity(velocity);I want the velocity to slow down gradually in relation to the distance between current body position and target. Velocity should slow down more as the position gets closer to target. I want to control this with an Interpolation function.
This is to avoid overshooting the position. The returned velocity should never be greater than needed to reach the target position in the next frame.
This method should be able to smoothly push a body with it’s returned velocity even when the distance to target is very small like 0.01f
Assume you have access to, but not limited to: deltaTime, PhysicsSystem.STEP_FREQUENCY (when physics timestep = 1/60f, this = 60f) float maxSpeed (in meters per second) Vector2 targetPosition Body body
- Assistant