如何将球扔出弧线?

发布于 2024-11-10 17:20:32 字数 743 浏览 4 评论 0原文

我试图将球扔出一条弧线,无论是向左还是向右的弧线。

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未蓝澄海的烟 2024-11-17 17:20:34

投掷时你有两个组成部分。

  1. 由于重力的魔力而产生的垂直加速度。这将是 ay。

  2. 水平分量:没有空气摩擦,这是恒定速度。

假设您扔球,在离开手的那一刻,球的速度为 v0 = (v0x, v0y) 并且位于位置 p0。那么 v0x 将始终保持不变。

球在时间 t 时的速度为 v(t) = (v0x, v0y + t * ay)

对于动画的每个刻度,将 deltat * v(t) 添加到球的当前位置,您应该设置。

每次球弹起时,您都应该在其弹起的表面上镜像其速度矢量,并减去其总能量的一定百分比(Ekin + Epot,尽管如果它在地面上且在地面上,Epot 将为 0) gound 为零电势),以获得对数弹跳。

如果您也想要空气摩擦力,只需在每个动画周期中减去总能量的一定百分比即可。

这里有一些代码,不是 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)

When throwing you have two components.

  1. A vertical acceleration due to the magics of gravity. This will be ay.

  2. 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):

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)
忆梦 2024-11-17 17:20:34

方程为(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 为负。

The equations are (V = velocity, t = time elapsed, x0, y0 = coordinates of launch point):

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

You don't need to distinguish between left and right, for one direction V is positive, for the other V is negative.

焚却相思 2024-11-17 17:20:34

有几件事:(免责声明:我根本不熟悉动作脚本,但我制作了一些需要投掷弧线的游戏。)

首先,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文