如何将球扔出弧线?
我试图将球扔出一条弧线,无论是向左还是向右的弧线。
这是我的代码:
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;
它看起来根本不像球在“弧线”,它看起来更像是一条对角线?
I am trying to throw a ball in an arc, either an arc going left or right.
Here is my code:
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;
It doesn't really look like the ball is "arcing" at all, it seems to be more of a diagonal line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
投掷时你有两个组成部分。
由于重力的魔力而产生的垂直加速度。这将是 ay。
水平分量:没有空气摩擦,这是恒定速度。
假设您扔球,在离开手的那一刻,球的速度为 v0 = (v0x, v0y) 并且位于位置 p0。那么 v0x 将始终保持不变。
球在时间 t 时的速度为 v(t) = (v0x, v0y + t * ay)
对于动画的每个刻度,将 deltat * v(t) 添加到球的当前位置,您应该设置。
每次球弹起时,您都应该在其弹起的表面上镜像其速度矢量,并减去其总能量的一定百分比(Ekin + Epot,尽管如果它在地面上且在地面上,Epot 将为 0) gound 为零电势),以获得对数弹跳。
如果您也想要空气摩擦力,只需在每个动画周期中减去总能量的一定百分比即可。
这里有一些代码,不是 ActionScript,但我希望可读。 (ctor 的参数都是 Vector2d;clone() 隐式使用,但你可以猜到它的作用):
When throwing you have two components.
A vertical acceleration due to the magics of gravity. This will be ay.
A horizontal component: Without air friction this is a constant velocity.
Let's say you throw the ball and at the moment of leaving your hand it has a velocity v0 = (v0x, v0y) and is at position p0. Then v0x will be constant for all time.
The speed of the ball at time t would be v(t) = (v0x, v0y + t * ay)
For each tick of your animation, add deltat * v(t) to the current position of the ball and you should be set.
Everytime the ball bounces, you should mirror its velocity vector on the surface it bounced and substract a certain percentage of its total energy (Ekin + Epot, although Epot will be 0 if it is on the ground and the gound is zero potential), in order to get a logarithmic bouncing.
If you want air friction too, just substract a certain small percentage of the total energy with every animation tick.
Here some code, not in ActionScript, but I hope readable. (The parameters to the ctor are both Vector2d; clone() used implicitly but you can guess what it does):
方程为(V = 速度,t = 经过的时间,x0, y0 = 发射点坐标):
您不需要区分左右,一个方向 V 为正,另一个方向 V 为负。
The equations are (V = velocity, t = time elapsed, x0, y0 = coordinates of launch point):
You don't need to distinguish between left and right, for one direction V is positive, for the other V is negative.
有几件事:(免责声明:我根本不熟悉动作脚本,但我制作了一些需要投掷弧线的游戏。)
首先,cos 和 sin 的界限都在 -1 和 1 之间。通常 x 以像素为单位绘制,所以将 x 更改 0.5 不会产生明显的变化。另外,由于 x 应该是 int,所以它甚至不会显示。
其次,计算此类物理现象所需的计算能力可能不是必需的——如果您正在进行精确的物理模拟,则可能是必需的。
还要考虑 Hyperboreus 的第二个评论:水平通常是恒定的。因此,假设这是横向卷轴游戏,您需要改变 y 分量而不是 x。
A couple things: (a disclaimer: I am not familiar with actionscript at all but I have made a couple games needing throwing arcs.)
First, cos and sin both have bounds between -1 and 1. Generally x is plotted in pixels, so changing x by 0.5 isn't going to make a difference visibly. Also, since x should be an int, it wouldn't even show.
Secondly, the computation power needed to compute this type of physics probably isn't necessary--maybe it is if you're doing an exact physics simulation.
Also consider Hyperboreus' second comment: horizontal is generally constant. Thus, assuming that this is side-scroller-esque, you would need to vary the y component and not the x.