# Some basic physics questions

This topic is 3277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi friends! I have some basic questions about physics engines, this is the first time I use one of them. Let's say I'm building a platform game using Box2D: If I want to program an enemy ( a body ) to move automatically until it collides and then turn around, I apply a force every frame ( inside my update method) and change force direction every time a collision happens. It seems to work, but my enemy accelerates to the infinity and beyond. How should I control this question? Maybe setting up linear velocity directly, but does not this avoid a proper physics simulation? I would like just my enemy have a small acceleration, and after that a constant velocity, How do it? What is the best way to managa the main character, apply force, apply linear velocity? About collisions, do you use sensors or just contact points? Thanks a lot for your time and help

##### Share on other sites
Just set a max-velocity value for the "enemy". If velocity passes above the give max-value, just set it to the maxvalue.

##### Share on other sites
Quote:
 Original post by ricardo_ruiz_lopezHow should I control this question? Maybe setting up linear velocity directly, but does not this avoid a proper physics simulation?I would like just my enemy have a small acceleration, and after that a constant velocity, How do it?What is the best way to managa the main character, apply force, apply linear velocity? About collisions, do you use sensors or just contact points?

That's a good point, especially when you start getting involved with constrains which manipulate angular and linear velocity themselves.

It is usually best to act on forces instead so you do not violate the rigid body simulation. To control the velocity of a player, or set a terminal velocity, you can add a force acting in the opposite direction of the velocity, that is proportional to the 'excess' you are getting. In another word, fluid friction (which can be a linear ration, exponential, ect...). In an ideal case, an object would be capable of accelerating to infinity (well, the speed of light). What prevents an object to do so are constraints, frictions, and collisions.

Friction is nice, as the amount of friction increases as the velocity increases, so it doesn't have a 'cliff edge' effect, but it's harder to actually control precisely what terminal velocity you want your object to have.

To have more accuracy on controlling the velocity, you should be able to apply an impulse (an instant force if you will) on an object.

say terminal velocity would be 10 meters/sec.

your current velocity at the end of the frame is 15 meters/sec.

apply an impulse (-5 * mass) to the object.

##### Share on other sites
I implemented nomonkeybusiness solution in a few minutes and it seems it's working perfecly, but as oliii says, it is better not to violate physics simulation.

BTW, is anyone using BOX2D? How do you implement JUMP? Do you see if character is on the floor (there are contact) and then aplly an impulse? My question is about floor, how do you detect it? or do you use sensors?

Thanks for help.

##### Share on other sites
Also, second point about velocity clamping. Obviously, if a player is on, say, a train, His terminal velocity will be affected by the train speed as well. So it can be tricky to determine what the actual speed of the player should be (probably his relative velocity against the floor, but then what if he is safely in the train and stars to jump around!).

sensors are what I've seen used in the past to detect the position of the floor underneath a player.

That allows you to deal with the case of going downstairs and down a slope. If you just do a rigid body sim, your character will basically fly momentarily, making the controls very floaty and not very precise.

I would implement the jump as you suggest. You need a 'on floor' state in the character, that will allow you to apply an impulse on the character, weld him to the floor underneath his feet, and navigate small vertical obstacles (stairs).

However, I would implement the jump, over time. A single frame impulse will look pretty weird, as it will need to be very large to combat gravity, so you can jump to a given height. A decaying jump impulse will give you a much nicer jump mechanics, as well as the possibility to control the height you wanna jump up to (checking the jump key pressed during the jump phase).

##### Share on other sites
Quote:
 Original post by oliiiAlso, second point about velocity clamping. Obviously, if a player is on, say, a train, His terminal velocity will be affected by the train speed as well.

Good point. Instead of getting linear velocity relative to the world, maybe we can get linear velocity relative to the floor ( or train ) using

b2Vec2  GetLinearVelocityFromWorldPoint (const b2Vec2 &worldPoint) const    Get the world linear velocity of a world point attached to this body.  b2Vec2  GetLinearVelocityFromLocalPoint (const b2Vec2 &localPoint) const    Get the world velocity of a local point.

oliii, are you using box2d?

nope :)

• 10
• 16
• 14
• 18
• 15