move X,Y position in physics

  ... the Physics Box2D Extension for AndEngine.

move X,Y position in physics

Postby johnas » Tue Apr 03, 2012 12:46 am

Hello
I am trying to position a body using the physics engine with this code:

float touchX = pp[0];
float touchY = pp[1];
float diffX = touchX - mBody.getPosition().x;
float diffY = touchY - mBody.getPosition().y;
mBody.setLinearVelocity(new Vector2(( pp[0]) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT , ( pp[1]) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT));

The X and Y coords are from a TMX tile map and are 32 pixels square, I am trying to get the body in the physics engine to move to certain tiles in a tile map (held in the pp[] array) but instead it moves to a random chaotic path. Is this the right approach to convert X,Y coords and move to that point??
Thanks
johnas
 
Posts: 10
Joined: Mon Jan 16, 2012 3:06 pm

Re: move X,Y position in physics

Postby OzLark » Tue Apr 03, 2012 1:12 am

When you say position, do you want it to instantly appear at the new x,y location or do you want it to travel there over time?

If you want the object to instantly appear you use a transform like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. sprite.getBody().setTransform(x/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, y/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, 0);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Depending on what you're trying to do. But to move an object (make it travel from location A to B over time) you would usually use a move modifier on the sprite/Entity and not use physics - but that depends on what you're trying to do.
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: move X,Y position in physics

Postby johnas » Tue Apr 03, 2012 10:36 am

Hello
Thanks for the reply. I did try setTransform() which works as expected, however following a number of X,Y coordinates it moves too quickly and so appears to arrive at the end X,Y coordinate instantly. So yes I am looking to move it gradually along the X,Y axis so it travels there -- is this not normally done in the physics world? or would I need to use the movemodifier() you mentioned?? Thanks
johnas
 
Posts: 10
Joined: Mon Jan 16, 2012 3:06 pm


Return to Physics Box2D

Who is online

Users browsing this forum: No registered users and 9 guests