當你扔有兩個組件。
由於重力的魔力引起的垂直加速度。這將是唉。
水平分量:沒有空氣摩擦,這是一個恆定的速度。
比方說,你扔球,並在離開你的手它有一個速度v0 =(v0x,v0y),並在位置p0。那麼v0x將始終保持不變。
球的在時刻t的速度將是V(T)=(V0X,v0y + T * AY)
對於動畫的每個蜱,添加的DeltaT * V(t)的當前位置的球,你應該被設置。
每次球反彈,你應該反映其速度矢量它反彈的表面上和。減去其總能量(鄭伊健+ Epot的一定百分比,雖然Epot將爲0,如果它是在地面上和gound是零電位),以獲得對數反彈。
如果您還想要空氣摩擦,只需減去總能量的某個小的百分比與每個動畫節拍。
這裏有一些代碼,不在ActionScript中,但我希望可讀。 (在構造函數中的參數都是的Vector2D;克隆()隱式地使用,但你能猜出它做什麼):
class Vector2d:
def __init__ (x, y):
self.x = x
self.y = y
def add (other):
self.x += other.x
self.y += other.y
def mulScalar (scalar):
self.x *= scalar
self.y *= scalar
def mulVector (vector) # NOT the cross product
self.x *= vector.x
self.y *= vector.y
class BouncingBall:
AGRAV = ? #gravitational acceleration (mg)
DELTAT = ? #time between ticks
ELASTICITY = ? Elasticity of ball/floor
def __init__ (self, pos, v):
self.pos = pos
self.v = v
def tick (self):
deltapos = self.v.clone()
deltapos.mulScalar (DELTAT)
self.pos.add (deltapos)
if self.pos.y <= 0: #bounce
self.pos.y = 0 #adjust ball to ground, you need to choose DELTAT small enough so nobody notices
self.v.mulVector (1, -1) #mirror on floor
self.v.mulScalar (ELASTICITY)
self.v.add (0, AGRAV * DELTAT)
您需要添加一個重力常量,該重力常數將應用於您的速度值。此外,您只需在球最初投擲時設定一個角度。 – 2011-05-27 17:44:22
嗨,我更新了我的代碼以反映這些建議,但它似乎仍然是直線移動(而不是曲線)? – redconservatory 2011-05-27 17:50:31
另外,有沒有辦法讓它左「左」和右右「右」? – redconservatory 2011-05-27 17:50:49