gpt4 book ai didi

LibGdx 弓箭游戏物理

转载 作者:行者123 更新时间:2023-12-01 06:26:42 26 4
gpt4 key购买 nike

我正在开发一个用 LibGdx 引擎和 Java 编写的新游戏。
我在这个游戏中遇到了一些物理问题。

我想以弹道轨迹射箭(愤怒的小鸟风格)
并且找不到这样做的方程式。

我正在使用这些速度方程:

    float velx = (float) (Math.cos(rotation) * spd);
float vely = (float) (Math.sin(rotation) * spd);

我将其添加到当前位置,箭头朝一个方向射出 - 直线。

我想也许改变旋转会帮助我实现我想要的(弹道路径)。

它确实有帮助,但我也想拥有轨迹。

我看见了这个
有人已经发布但不知道如何使用它的 ProjectileEquation 类:
 public class ProjectileEquation
{

public float gravity;
public Vector2 startVelocity = new Vector2();
public Vector2 startPoint = new Vector2();
public Vector2 gravityVec = new Vector2(0,-10f);

public float getX(float n) {
return startVelocity.x * (n ) + startPoint.x;
}

public float getY(float n) {
float t = n;
return 0.5f * gravity * t * t + startVelocity.y * t + startPoint.y;
}

}

我正在寻找一些帮助来帮助我将这个类用于弹道轨迹。

这是我尝试使用它的方式:
    for(int i =0;i<30;i++)
{
Texture f = ResData.Square_1;
ProjectileEquation e= new ProjectileEquation();
e.gravity = 1;
e.startPoint = new Vector2(bow.getX(),bow.getY());//new Vector2(-bow.getX(),-bow.getY()); //My bow is opposite so it suppose to work fine
e.startVelocity = getVelocityOf(bow.getRotation());
Vector3 touchpos = new Vector3();

s.draw(f,e.getX(i) ,e.getX(i),5,5);

}

最佳答案

您发布的 ProjectileEquation 类看起来会在给定时间增量的情况下计算 X 和 Y 位置,因此您传入的浮点数应该是自您开始移动箭头(以秒为单位)以来的时间增量。

但是,该代码不会为您提供箭头的角度。要找到这一点,我建议您保留之前的 X 和 Y,然后您可以使用 Math.atan2() 根据之前的 XY 和当前的 XY 计算角度。谷歌 atan2 获取有关如何使用它的大量信息。

然而,最好的方法是使用 Box2d 并正确建模场景。那么你根本不需要参与数学。我在某处读到这就是 Angry Birds 所使用的,并且是对此类物理游戏进行建模的绝佳选择。

我希望你的游戏一切顺利。

关于LibGdx 弓箭游戏物理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27668520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com