Page 1 of 1

How to Stop and Go the entity?

Posted: Sun Aug 28, 2011 12:53 am
by piropiro
I want to "Stop and Go" the specific entity.
This code is correct physically?
Or there any other easier way?

struct EntityMotion
{
public Vector3 vel;
public Vector3 ang;
public float dam;
public Vector3 mom;
public bool grav;
}


protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

GamePadState padState = GamePad.GetState(PlayerIndex.One);

//stop! even number index entity
if (padState.Buttons.A == ButtonState.Pressed
&& oldPadState.Buttons.A == ButtonState.Released)
{
entityMotions.Clear();

for (int i = 0; i < space.Entities.Count; i++)
{
if (i % 2 == 1)continue;

EntityMotion em = new EntityMotion();
em.vel = space.Entities.LinearVelocity;
em.ang = space.Entities.AngularVelocity;
em.dam = space.Entities.LinearDamping;
em.mom = space.Entities.LinearMomentum;
em.grav = space.Entities.IsAffectedByGravity;

entityMotions.Add(em);

space.Entities.LinearVelocity = Vector3.Zero;
space.Entities.AngularVelocity = Vector3.Zero;
space.Entities.LinearDamping = 0;
space.Entities.LinearMomentum = Vector3.Zero;
space.Entities.IsAffectedByGravity = false;
}
}

//go! even number index entity
if (padState.Buttons.B == ButtonState.Pressed
&& oldPadState.Buttons.B == ButtonState.Released)
{
int j = 0;

for (int i = 0; i < space.Entities.Count; i++)
{
if (i % 2 == 1) continue;

space.Entities[i].LinearVelocity = entityMotions[j].vel;
space.Entities[i].AngularVelocity = entityMotions[j].ang;
space.Entities[i].LinearDamping = entityMotions[j].dam;
space.Entities[i].LinearMomentum = entityMotions[j].mom;
space.Entities[i].IsAffectedByGravity = entityMotions[j].grav;

j++;
}
}
space.Update();
oldPadState = padState;
base.Update(gameTime);
}

Re: How to Stop and Go the entity?

Posted: Sun Aug 28, 2011 1:40 am
by Norbo
This code is correct physically?
Since the operation is fundamentally non-physical, being physically correct isn't meaningful- does it do what you want?

If it doesn't do what you want, then I'll probably need more information about what your goal is so I can provide a better recommendation.

However, I can tell you that storing and resetting the linear momentum along with the linear velocity is unnecessary. They are equivalent. Linear momentum = linear velocity * mass.

Further, LinearDamping saps energy from the entity over time. Setting it to zero when you're trying to 'stop' an entity is unnecessary.

Re: How to Stop and Go the entity?

Posted: Sun Aug 28, 2011 9:30 am
by piropiro
thank you reply.
If it doesn't do what you want, then I'll probably need more information about what your goal is so I can provide a better recommendation.
my goal is below.
・static entity can collide with other movable entities.
・static entity doesn't move (keep stopping) even if it collides with other movable entities.

Is it possible?

The code previously presented,
Static entity moves when colliding with other movable entity.

Re: How to Stop and Go the entity?

Posted: Sun Aug 28, 2011 7:50 pm
by Norbo
Changing the dynamic entity to kinematic would be the way to go. You could use call the entity's BecomeKinematic method to do this. Once you want to unfreeze it, call BecomeDynamic again.

Kinematic entities have infinite inertia and mass, so they do not respond to any collisions.

Note that going to kinematic wipes out the mass and inertia tensor of the entity, and they'll have to be recalculated. If you cache the Mass, LocalInertiaTensor, and Volume of the entity and pass it into the BecomeDynamic method, it will avoid recalculating it and so it will run faster.

Re: How to Stop and Go the entity?

Posted: Mon Aug 29, 2011 6:41 am
by piropiro
Thank you!
I'll try it.