gpt4 book ai didi

java - 炮弹游戏数学

转载 作者:行者123 更新时间:2023-12-01 11:40:09 25 4
gpt4 key购买 nike

我必须用 Java 编写一个使用以下数学公式的游戏:

x = x1 + vt(cos(theta))
y = y1 - vt(sin(theta)) – 0.5gt2

模拟从地面“发射”的物体的运动。保存在名为“Canon Game”的文件夹中。问题是任何与数学有关的东西......都不起作用。

// The "Cannongame" class.
import java.awt.*;
import hsa.Console;
import java.lang.Math;

public class Cannongame
{

static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();
c.println("Enter intial angle(0-90): ");
c.println("Enter velocity(10-40): ");

c.fillRoundRect(50,125,80,25,20,50);
c.fillRect(70,103,30,25);
c.fillRect(100,105,60,15);
double xreal, yreal, v;
double the,theta;
long x,y;
v = c.readInt();
the = c.readInt();
theta = (the * 0.017453292);
double cos = Math.cos(Math.toRadians(v));
double sin = Math.sin(Math.toRadians(v));
for (int t = 1; t <= 100; t++)
{
xreal = 100+(v*t*cos(theta));
yreal = 200-(v*t*sin(theta)-0.5*1*t*t);
x = Math.round(xreal);
y = Math.round(yreal);
c.setColor (Color.black);
c.fillOval(x, y, 5, 5);
}
// Place your program here. 'c' is the output console
} // main method
} // Cannongame class

最佳答案

对于所有角度,请尝试使用:

Math.toDegrees(yourAngle);

您期望的角度是规则的“度”。 java中的普通sin、cos、tan函数返回弧度。

关于java - 炮弹游戏数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596612/

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