question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Box2D setTransform angle not persisting

See original GitHub issue

Issue 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MobiDevelopcommented, May 8, 2020

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.

0reactions
mgsx-devcommented, May 10, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Box2D setTransform() angle is VERY incorrect - Stack Overflow
I have a box2D body that has two polygon fixtures attached to it. When I call setTransform (passing in the values 0,0 for...
Read more >
setTransform() Box2D physics not working
I'm working on a top-down shooter game in LibGDX and Box2D (so gravity is set to (0, 0) ). At the moment, to...
Read more >
Rotating to a given angle - Box2D tutorials - iforce2d
This is as simple as using SetTransform to set the angle, but first we need to know what the angle should be, given...
Read more >
org.jbox2d.dynamics.Body.setTransform java code examples
Manipulating a * body's transform may cause non-physical behavior. ... public void setTransform (Vector2 position, float angle) { tmp.set(position.x, ...
Read more >
Dynamics Module - Box2D
You may also specify the body's angle in radians, which is not affected by the position ... bool b2Body::SetTransform(const b2Vec2& position, float angle);....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found