0

我在合成高级对象投影公式时遇到了一些麻烦。我已经想出了一些基本的物理模拟公式,例如:

  • 物体的速度 x += cos(angle); y += sin(angle); *可以通过鼠标位置或tan(...目标和初始值)获得角度

但这只根据角度直线行进。

  • 重力 Yvelocity = Yvelocity - gravity; if(!isHitPlatform) { Obj.y += YVelocity }

  • 弹跳// No point if we've not been sized... if (height > 0) { // Are we bouncing... if (bounce) { // Add the vDelta to the yPos // vDelta may be postive or negative, allowing // for both up and down movement... yPos += vDelta; // Add the gravity to the vDelta, this will slow down // the upward movement and speed up the downward movement... // You may wish to place a max speed to this vDelta += gDelta; // If the sprite is not on the ground... if (yPos + SPRITE_HEIGHT >= height) { // Seat the sprite on the ground yPos = height - SPRITE_HEIGHT; // If the re-bound delta is 0 or more then we've stopped // bouncing... if (rbDelta >= 0) { // Stop bouncing... bounce = false; } else { // Add the re-bound degregation delta to the re-bound delta rbDelta += rbDegDelta; // Set the vDelta... vDelta = rbDelta; } } } }

我需要帮助方法来结合这三个公式来创建一个高效且轻量级的算法,该算法允许将对象投射到由角度确定的拱形中,但在停止之前继续反弹几次,所有这些都具有可接受的数量每个点之间的不连续性。*注意:让手榴弹由 af(x) = -x^2 公式确定会随着斜率增加而产生更大的跳跃不连续性,迫使您反转公式以找到 x = +-y 值(以确定是 + 还是 - ,检查边界)。

4

1 回答 1

1

就像是:

class granade
{
    private static final double dt = 0.1; // or similar

    private double speedx;
    private double speedy;
    private double positionx;
    private double positiony;

    public granade(double v, double angle)
    {
        speedx = v * Math.cos(angle);
        speedy = v * Math.sin(angle);

        positionx = 0;
        positiony = 0;
    }

    public void nextframe()
    {
        // update speed: v += a*dt
        speedy -= gravity* dt;

        // update position: pos += v*dt
        positionx += speedx * dt;
        double newpositiony = positiony + speedy*dt;

        // bounce if hit ground
        if (newpositiony > 0)
            positiony = newpositiony;
        else {
            // bounce vertically
            speedy *= -1;
            positiony = -newpositiony;
        }
    }

    public void draw() { /* TODO */ }
}

OT:避免使用 Math.atan(y/x),使用 Math.atan2(y, x)

于 2014-07-18T06:54:25.167 回答