开发者

applying friction to objects

开发者 https://www.devze.com 2023-03-06 09:27 出处:网络
I have a ball that moves aro开发者_开发百科und the screen depending on what way my phone is tilting. How do I apply friction to the ball so that when the phone is lying flat, the ball will eventually

I have a ball that moves aro开发者_开发百科und the screen depending on what way my phone is tilting. How do I apply friction to the ball so that when the phone is lying flat, the ball will eventually come to a stop?


Without knowing the level of detail of your model or your exact methods, I'd say the best way to do this is to think of friction as a vector which has a small constant magnitude and always points in the direction opposite your ball's velocity vector. You can come up with suitable magnitude values for different simulated surface types just by experimentation. Each time you update the ball's position and velocity, simply include this friction vector in the calculation.


Friction is a force that acts opposite of the motion, so you just need to create a vector representing the friction which is pointed in the opposite direction of your velocity vector.

The size of the friction vector decides how quickly the object will come to a halt. It is a constant value based on the material of the moving object and the stuff it is moving on. Just make something up which looks nice for your purpose.

Add the friction vector to the acceleration vector (which is zero when the iphone is flat on the table). Now just do interpolation as usual and it will eventually cause the object to stop moving.

(Btw, when the object is not moving the friction vector should be set to zero)


Better to use Box2d For getting Physics motion

In box2d for getting friction...

use this

b2fixtureDef *fd;
fd->friction = 0.7;(or whatever)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号