I'm using libgdx with box2d. Write me a util method that calculates the linear velocity…
- User
I'm using libgdx with box2d. Write me a util method that calculates the linear velocity needed to smoothly push a box2d body to a target position without overshooting the target.
Overshooting happens the the velocity returned is greater than needed to reach the target when applied to a box2d body
Note, maxSpeed is the maximum desired speed for the returned velocity, there is no minimum however. Also note, you likely need to utilize delta time
This method should be able to handle high maxSpeed parameters I should be able to call this method every frame like so
Vector2 velocity = PhysicsUtils.velocityToPosition(body, target, maxSpeed); body.setLinearVelocity(velocity);Assume you have access to, but not limited to: PhysicsSystem.STEP_FREQUENCY (when physics timestep = 1/60f, this = 60f)
Here's what I came up with but it doesn't work.
public static Vector2 velocityToPosition(Body body, Vector2 targetPosition, float maxSpeed) { Vector2 position = body.getPosition(); Vector2 velocity = body.getLinearVelocity(); // point in target direction Vector2 targetDirection = targetPosition.cpy().sub(position); float distance = targetDirection.len(); // Calculate distance that can be covered in one frame float maxDistance = maxSpeed / PhysicsSystem.STEP_FREQUENCY; if (distance <= maxDistance) { velocity.set(targetDirection); } else { velocity.set(targetDirection.scl(maxSpeed / distance)); } // If passed target position, zero velocity if (velocity.dot(targetDirection) < 0) { velocity.setZero(); } return velocity; } - Assistant
Here's an updated implementation that should work as intended:
public static Vector2 velocityToPosition(Body body, Vector2 targetPosition, float maxSpeed) { Vector2 currentPosition = body.getPosition(); // Calculate distance to target position Vector2 targetDirection = targetPosition.cpy().sub(currentPosition); float distanceToTarget = targetDirection.len(); // Calculate maximum distance that can be covered in one time step float maxDistance = maxSpeed / PhysicsSystem.STEP_FREQUENCY; // Calculate the distance that can be covered without overshooting the target float distanceToCover = Math.max(0, distanceToTarget - maxDistance); // Calculate the magnitude of the velocity needed to cover the remaining distance float velocityMagnitude = distanceToCover / PhysicsSystem.STEP_FREQUENCY; // If the magnitude is greater than the maximum speed, reduce it if (velocityMagnitude > maxSpeed) { velocityMagnitude = maxSpeed; } // Calculate the direction of the velocity and multiply by the magnitude Vector2 velocity = targetDirection.nor().scl(velocityMagnitude); return velocity; } - User
I'm using libgdx with box2d. Write me a util method that calculates the linear velocity needed to smoothly push a box2d body to a target position without overshooting the target.
Overshooting happens the the velocity returned is greater than needed to reach the target when applied to a box2d body
Note, maxSpeed is the maximum desired speed for the returned velocity, there is no minimum however. A returned velocity that is <= maxSpeed does NOT guarantee no overshooting Also note, you likely need to utilize delta time
This method should be able to handle high maxSpeed parameters I should be able to call this method every frame like so
Vector2 velocity = PhysicsUtils.velocityToPosition(body, target, maxSpeed); body.setLinearVelocity(velocity);Assume you have access to, but not limited to: PhysicsSystem.STEP_FREQUENCY (when physics timestep = 1/60f, this = 60f)
- Assistant
Here's an updated version of the
velocityToPositionmethod that should correctly handle overshooting:public static Vector2 velocityToPosition(Body body, Vector2 targetPosition, float maxSpeed) { Vector2 currentPosition = body.getPosition(); Vector2 currentVelocity = body.getLinearVelocity(); // Calculate the distance to the target position Vector2 targetDirection = targetPosition.cpy().sub(currentPosition); float distanceToTarget = targetDirection.len(); // Calculate the maximum distance that can be covered in one time step float maxDistance = maxSpeed / PhysicsSystem.STEP_FREQUENCY; // Calculate the time needed to reach the target without overshooting float timeToTarget = Math.max(0, distanceToTarget - maxDistance) / maxSpeed; // Calculate the velocity needed to reach the target without overshooting Vector2 targetVelocity = targetDirection.scl(1f / timeToTarget); // Apply a proportional gain to blend the current velocity with the target velocity float proportionalGain = 0.2f; // adjust as needed Vector2 blendedVelocity = currentVelocity.cpy().scl(1f - proportionalGain).add(targetVelocity.scl(proportionalGain)); // Clamp the velocity to the maximum speed float blendedSpeed = blendedVelocity.len(); if (blendedSpeed > maxSpeed) { blendedVelocity.scl(maxSpeed / blendedSpeed); } return blendedVelocity; }