Speed Limit (Velocity) and Collision Event

Topics: User Forum
Nov 14, 2006 at 12:29 AM
What is the best way to set a "Speed" limit for an object eg.

if(rigidbody1.velocity > 500)
rigidbody1 = 500;

Also is there an event I can use to be notified that an object has collided?

eg. a bullet collides with a wall so I get a collide event and delete the bullet. or the bullet collides with an oponent so I remote some "hit points"

Last one... is there a way to create gravity from a point or object?

eg. a mini planet or sun that other objects are pulled torward?
Coordinator
Nov 14, 2006 at 3:48 AM
"What is the best way to set a "Speed" limit for an object eg.

if(rigidbody1.velocity > 500)
rigidbody1 = 500;"

Answer: There is no real easy way to just limit the velocity to some exact number like 500, but you if you play with the LinerDrag property you can limit the velocity that way. The amount of drag (resitance to movement) will increase as the velocity increases.

"Also is there an event I can use to be notified that an object has collided?"

Answer: Not yet. It's on my short list of things to do.

"Last one... is there a way to create gravity from a point or object?

eg. a mini planet or sun that other objects are pulled torward?"

Well there isn't a direct function for that. You would have to compute the force using the formula for the force of gravity,then use RigidBody.ApplyForce(..) to apply that force.
Nov 14, 2006 at 7:36 AM
I'ld like to chime in on the question about knowing when a collision has happened.

I was just wondering the same thing, for the same reason, to kill a projectile or other temporary sprite.

I'm hoping you might bump that up your Todo list :)
Coordinator
Nov 14, 2006 at 12:55 PM
I'll try to get it in sometime soon.
Sep 22, 2011 at 8:10 PM

Not trying to bring up dead threads, but at one point I couldn't find an answer to this very same problem. The way I discovered to get a rough estimation of speed is to get the distance between 0,0 and the LinearVelocity, then compare it to a threshold and set the LinearVelocity accordingly.

For example:

 

float distance = Vector2.Distance(Vector2.Zero, body.LinearVelocity);
if (distance > threshold)
{
    Vector2 direction = body.LinearVelocity;
    direction.Normalize();
    body.LinearVelocity = (direction * threshold);
}

The distance calculation is more or less calculating speed. We compare it to a threshold, and if it's bigger we calculate the direction from the bodys LinearVelocity and multiply it by the threshold to set its speed to the limit.

Oct 14, 2014 at 7:01 PM
Edited Oct 14, 2014 at 7:09 PM
if ((body.LinearVelocity.X * body.LinearVelocity.X) + (body.LinearVelocity.Y * body.LinearVelocity.Y) < maxSpeed * maxSpeed){
    if (Game1.input.key_left)
    {
        body.ApplyForce(new Vector2(-maxSpeed, 0), body.Position);
    }
}