40023

Box2DDebugRenderer and SpriteBatch misplaced

Question:

I am playing with libgdx for making yet another physics game :) and I have found something weird. Namely I use SpriteBatch for rendering images at the same time with Box2DDebugRenderer for debuging.

But when the physics acts, they appear to be misplaced. I wrote:

public class Canon implements ApplicationListener { private OrthographicCamera camera; private Box2DDebugRenderer debugRenderer; /... public void create() { camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT); world = new World(new Vector2(0f, -9.8f), true); camera.position.set(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, 0f); camera.update(); debugRenderer = new Box2DDebugRenderer(); spriteBatch = new SpriteBatch(); //Create a canon. A rectangle :) bd = new BodyDef(); fd = new FixtureDef(); fd.density = 1; PolygonShape ps = new PolygonShape(); // Cannon bd.type = BodyDef.BodyType.StaticBody; bd.position.set(new Vector2(8, 5)); ps.setAsBox(5f, 1f); cannonBody = world.createBody(bd); fd.shape = ps; cannonBody.createFixture(fd); } @Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); debugRenderer.render(world, camera.combined); world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS); spriteBatch.begin(); Sprite s = (Sprite)targetBody1.getUserData(); spriteBatch.draw(s.getTexture(), (targetBody1.getPosition().x - bodyWidth/2)*ppuX, (targetBody1.getPosition().y - bodyheight/2)*ppuY, 0f, 0f, bodyWidth*ppuX, bodyheight*ppuY, 1f, 1f, radToGrad*targetBody1.getAngle(), 0, 0, s.getTexture().getWidth(), s.getTexture().getHeight(), false, false); spriteBatch.end(); } }

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/z9BbN.png" data-original="https://i.stack.imgur.com/z9BbN.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

And here's how it looks thereafter<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/kQZgX.png" data-original="https://i.stack.imgur.com/kQZgX.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Any ideas?

Thanks!

Answer1:

I found it. This is due to the fact rotations in OpenGL are done around the bottom left corner, whereas rotations in Box2D are done around mass center's body. Rotating the texture around mass center body gives right physics/texture behavior.

Recommend

  • On disconnecting ChromeCast from one device it is getting disconnected from both android devices
  • XNA - Drawing Strings on Menus
  • How to create a histogram based on true or false in R?
  • Changing fireDate of a repeating NSTimer
  • XNA - How to dim a section of the screen?
  • LIBGDX ScissorStack Example?
  • XNA - How to change orientation of Drawing a List of Strings
  • plot if col A has substring
  • Blackberry - Custom EditField Cursor
  • NHibernate Validation Localization with S#arp Architecture
  • Body moving without any force applied? (Box2d)
  • ilmerge with a PFX file
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Adding a button at the bottom of a table view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Read text file and split every line in MSBuild
  • req.body is undefined - nodejs
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Counter field in MS Access, how to generate?
  • Get object from AWS S3 as a stream
  • Java applet as stand-alone Windows application?
  • MySQL WHERE-condition in procedure ignored
  • Adding custom controls to a full screen movie
  • Comma separated Values
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Error creating VM instance in Google Compute Engine
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • Understanding cpu registers
  • how does django model after text[] in postgresql [duplicate]
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • Add sale price programmatically to product variations
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?