Box2D setTransform angle not persisting
See original GitHub issueIssue details
When the angle of a Box2D Body is set using setTransform
the angle does not persist when the call is not made. If the setTransform
is not called the angle will snap back to 0, if setTransform
is called every frame with the target angle set the body’s rotation remains correct.
Click here for a short video demonstrating the bug (the texture won’t rotate, only the box drawn by a Box2D debug render should). The snippet below is used to move the body to the target location on click.
Reproduction steps/code
targetPosition
is a Vector2
and bodyPosition
is equal to body.getPosition()
.
// https://stackoverflow.com/a/23482465/3833743
Vector2 direction = targetPosition
.sub(bodyPosition)
.nor();
body.setTransform(bodyPosition, direction.angleRad());
body.setLinearVelocity(direction.scl(target.speed));
The desired state is that the body points to and move towards and the target position. The above snippet is run until the body arrives at the target location as which point the body remains at the location angle snaps to 0.
Version of LibGDX and/or relevant dependencies
libGDX: 1.9.10
Please select the affected platforms
- Android
- iOS (robovm)
- iOS (MOE)
- HTML/GWT
- Windows
- Linux (Pop!_OS 20.04)
- MacOS
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Given the fact libgdx simply passes the transform to the underlying box2d native code, I’m not sure there’s actually a bug here - it appears to just be how box2d works.
for people facing this issue, setTransform should only be used to “teleport” bodies (rarely used). In order to manually control position and/or rotation, you should use setLinearVelocity and/or setAngularVelocity instead.