我正在开发一个用 LibGdx 引擎和 Java 编写的新游戏。我在这个游戏中遇到了一些物理问题。
我想以弹道轨迹(愤怒的小鸟风格)射箭,但找不到这样做的方程式。
我正在使用这些速度方程:
float velx = (float) (Math.cos(rotation) * spd);
float vely = (float) (Math.sin(rotation) * spd);
Run Code Online (Sandbox Code Playgroud)
我将其添加到当前位置,箭头朝一个方向射出 - 直线。
我想也许改变旋转会帮助我实现我想要的(弹道路径)。
它确实有帮助,但我也想拥有轨迹。
我看到有人已经发布了这个 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;
}
Run Code Online (Sandbox Code Playgroud)
}
我正在寻找一些帮助来帮助我将这个类用于弹道轨迹。
这是我尝试使用它的方式:
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);
}
Run Code Online (Sandbox Code Playgroud)
您发布的 ProjectileEquation 类看起来会计算给定时间增量的 X 和 Y 位置,因此您传入的浮点数应该是自箭头开始移动以来的时间增量(以秒为单位)。
但该代码不会给你箭头的角度。为了找到这一点,我建议你保留之前的X和Y,然后你可以使用Math.atan2()根据之前的XY和当前的XY来计算角度。Google atan2 获取大量有关如何使用它的信息。
然而,最好的方法是使用 Box2d 并正确建模场景。那么你根本就不必参与数学。我在某处读到这就是《愤怒的小鸟》所使用的,并且是对此类物理游戏进行建模的绝佳选择。
我希望你的比赛顺利。
| 归档时间: |
|
| 查看次数: |
883 次 |
| 最近记录: |