问题描述
|
我正在尝试将球扔向弧线,或者向左或向右弯曲。
这是我的代码:
var gravity = 2;
this.veLocity.y += gravity;
_angle = 5;
var theta:Number;
switch(_direction) {
case \"left\":
theta = _angle * Math.PI/180;
this.veLocity.x = Math.cos(theta) - Math.sin(theta);
break;
case \"right\":
theta = _angle * Math.PI/180;
this.veLocity.x = Math.cos(theta) - Math.sin(theta)
break;
}
this.x += this.veLocity.x;
this.y += this.veLocity.y;
看起来好像球根本不是“弧线”,似乎更像是对角线?
解决方法
投掷时有两个部分。
由于重力的作用,垂直加速度。会的。
水平分量:没有空气摩擦,这是恒定速度。
假设您丢了球,在离开手时,它的速度为v0 =(v0x,v0y),并且处于位置p0。然后,v0x将一直保持恒定。
球在时间t的速度为v(t)=(v0x,v0y + t * ay)
对于动画的每个滴答,将deltat * v(t)添加到球的当前位置,就可以设置您了。
每次球反弹时,都应在其反弹的表面上镜像其速度向量,并减去一定比例的总能量(Ekin + Epot,尽管如果Epot在地面上且Gound为零电位,则Epot将为0),以获得对数跳动。
如果还需要空气摩擦,则在每个动画滴答中减去总能量的一小部分。
这里有一些代码,不是用ActionScript编写的,但希望可读。 (ctor的参数都是Vector2d;隐式使用了clone(),但您可以猜测它的作用):
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)
, 公式为(V =速度,t =经过的时间,x0,y0 =发射点的坐标):
x = x0 + Vt * cos(angle)
y = y0 + Vt * sin(angle) - (g * t^2) / 2
-------------
^
this is gravity effect,without this
the trajectory would be a line
您不需要区分左右,一个方向V为正,另一个方向V为负。
, 几件事:(免责声明:我一点都不熟悉动作脚本,但是我做了几场需要抛出弧线的游戏。)
首先,cos和sin的界限都在-1和1之间。通常x以像素为单位绘制,因此将x更改为0.5不会产生明显的差异。另外,由于x应该是一个int,所以它甚至不会显示。
其次,可能不需要计算这种类型的物理学所需的计算能力,如果您要进行精确的物理模拟,则可能不需要。
还要考虑Hyperboreus的第二条评论:水平通常是恒定的。因此,假设这是侧面滚动式的,则需要更改y分量而不是x。