python代码物理,Python物理库?

Are there any good up-to-date physics libraries for Python that are for Linux? I'm just getting into Python using PyGame, but PyGame's lack of a physics library isn't cool. I spent about two hours trying to find a good physics library but it's like trying to grab oil; I can't seem to do it.

I barely need a physics engine at all; all I want to do is program an object to 'jump' up and then fall back to the ground. There seems to be some simple collisions going on (which PyGame can handle, I think) but it's the actual jump calculation that's stumping me. If it turns out that there aren't any good ususable physics libraries, the problem seems simple enough that I might just try to find a basic acceleration equation and a gravity equation and try to apply those... I'd like to avoid having to do that, though.

Thanks for any help.

解决方案

The basic physics kinematic equations are all you need. Even though the questions was already answered, if I were you, I'd still do it by hand just because using a library seems like overkill. Start the equation for velocity:

velocity = initial velocity + (acceleration * time)

From there, we integrate to find position:

position = initial position + (initial velocity * time) + (acceleration * time^2)/2

Your jump is looking to calculate the y position of the character, so just use that equation to calculate y position and toy with initial velocity and acceleration. Standard acceleration due to gravity is -9.8 meters per second^2 (at least on the surface of the earth - it's different son different planets). Start with initial position whatever your character is at, or 0 if the ground is 0 to you.

So:

y = vt + (-4.9)t^2

Pick an initial velocity v value to provide the upward movement when the jump starts, and t should be the elapsed game time since he started jumping.

You only need one line of code to do this, no libraries necessary!

EDIT: dealing with what to do when you land.

So in the real world, acceleration is caused by unbalanced forces. Gravity is always acting on you, but when you're standing in the ground, it's being countered and canceled out by the "normal force" of the ground. As long as the ground you're standing on is strong enough to support your weight, the ground will push back up and counter gravity and you will not accelerate downward. So in your game, if you're not actually simulating forces, just change the acceleration from -9.8 to 0 when your character is touching the ground. If the ground is flat, your jump ends when position = initial position

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值