This project has moved. For the latest updates, please go here.

How change body.position

Topics: Developer Forum, Project Management Forum, User Forum
Mar 1, 2011 at 10:50 AM


I use the SimpleSamples and i try to change the position.

In the Demo 8 i have this:

_circle[i] = FixtureFactory.CreateCircle(World, 2f, 1f, _position, material);

_circle[i].Body.BodyType = BodyType.Dynamic;

_circle[i].Restitution = _restitution;

_position.X += 6f; 

_restitution += 0.2f;

My question is: How i change the circle[0] postion? Like: _circle[0].body.position.x = 0;


Thanks, Andor

Mar 1, 2011 at 11:26 AM
Edited Mar 1, 2011 at 11:30 AM

Position is a struct which is exposed as a public property in the Body class.  Since structs are of value-type, calling Position from the body returns a copy of the vector (by using the property's getter) in which you try to modify its x field therefore modifying (or at least trying to) a new copy of that vector.

If you want more information about this concept, check out the 'Fun with Structs' section here:

What you actually need to do is to create a new Vector2 with your new position and pass that to the property:

_circle[0].Body.Position = new Vector2(0, _circle[0].Body.Position.Y);

Jan 5, 2015 at 5:45 AM
Jan 5, 2015 at 4:59 PM
Use body.SetTransform() to teleport an objet to a position.
To simulate a phisic movement, use body.ApplyLinearImpulse() or body.ApplyForce() (If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity.)