【发布时间】:2013-04-05 01:16:49
【问题描述】:
我正在尝试为我的家庭作业创建一个类似愤怒的小鸟的游戏,但我在角度和弹丸运动方面遇到了问题。我给了它重力*时间模块,但它似乎没有进行抛射运动,这可能是什么问题?
float gravity = 9.75;
float time = 0;
float speedx=20;
float speedy=440;
int speed = 0;
int angle = 0;
String veloc = "";
void setup(){
size(800,600);
}
void keyPressed() {
if( key >= '0' && key <= '9' ){
veloc+=char(key);
if( abs( int( veloc ) ) > 1000 ){
speed = int(veloc.substring(0,2));
angle = int(veloc.substring(2,4));
println("Speed is " + speed + " m/sec");
println("Angle is " + angle + " degree");
time = time + 1;
}
}
}
void draw() {
background(129,201,255);
strokeWeight(0);
fill(64,152,3);
rect(0,200,800,600);
fill(188,133,61);
rect(400,375,150,30);
rect(400,500,150,30);
rect(400,275,150,30);
rect(400,400,30,120);
rect(520,400,30,120);
rect(400,275,30,120);
rect(520,275,30,120);
PImage slingshot;
slingshot = loadImage("slingshot.png");
image(slingshot, 30, 450);
PImage pig;
pig = loadImage("pig.png");
image(pig, 460, 253);
image(pig, 410, 253);
image(pig, 510, 253);
image(pig, 435, 353);
image(pig, 485, 353);
image(pig, 435, 478);
image(pig, 485, 478);
text("Please Type a Magnitude (0-99) and an Angle (0-99). E.g. 1045 , Speed = 10, Angle = 45", 120, 30);
PImage bird;
bird = loadImage("bird.png");
image(bird, speedx, speedy);
speedx = speedx + (speed * cos(angle));
speedy = speedy + (speed * -sin(angle)) + gravity*time;
}
【问题讨论】:
-
你没有清楚地列出问题;这就是你的问题的问题。
-
这里有一些提示......除非你想要抗风,否则水平速度是恒定的。你只需要真正担心正确的垂直距离计算——这将是你物理书中的“重力加速度”——减去初始速度。
-
我同意兰迪的观点。将您的速度视为两个部分(如果您需要将它们组合起来;以 arctan(verticalVelocity/horizontalVelocity) 作为角度)。设水平速度恒定,垂直速度受重力影响。像这样:
double vVelocity = 30;\, hVelocity = 2, gravAccel = 9.8; // obviously let them be whatever you want然后在你的更新速度方法中:`velocity -= gravAccel*updateTime/1000; // 假设 updateTime 以毫秒为单位,你也可以忘记,这取决于你。
标签: java processing angle