Processing 使用真实重力使对象跳跃一次
我希望我的对象(此处为矩形)跳跃一次,在上升过程中失去速度。当它几乎停下来的时候,我希望它能转回来,在下降的过程中加速。当它再次触地时,它应该停止。当我再次按下该键时,它也会这样做Processing 使用真实重力使对象跳跃一次,processing,physics,Processing,Physics,我希望我的对象(此处为矩形)跳跃一次,在上升过程中失去速度。当它几乎停下来的时候,我希望它能转回来,在下降的过程中加速。当它再次触地时,它应该停止。当我再次按下该键时,它也会这样做 我试图编写的代码实现了一个变量float gravity=0.75和一个跟踪速度的变量浮动速度=10 当速度大于1时,速度应该被矩形的Y坐标所抵消,然后速度应该降低,因为我将它乘以重力,它小于1 else if (keyCode == UP|| key == 'w') { while (speed >
我试图编写的代码实现了一个变量
float gravity=0.75代码>和一个跟踪速度的变量<代码>浮动速度=10代码>
当速度大于1时,速度应该被矩形的Y坐标所抵消,然后速度应该降低,因为我将它乘以重力,它小于1
else if (keyCode == UP|| key == 'w') {
while (speed >1 ) {
playerYPosition = playerYPosition-speed;
speed = speed * gravity;
}
重力大于1,但为负值,因此在最终结果中,数字I substrakt相加,矩形变小。为了获得速度,速度与重力相乘
gravity = -1.25;
speed = speed * gravity;
playerYPosition = playerYPosition-speed;
速度现在应该在-1.2…左右,因此它小于-1,而(…)
应该可以工作。再一次,它应该随着时间的推移而加速,直到达到起始速度,只是负数,以此作为起始点
while (speed < -1 && speed > -10) {
playerYPosition = playerYPosition-speed;
speed = speed * gravity;
因此,矩形并没有这样做,而是不断地向上跳(我认为)10个像素,仅此而已 这是整个代码块,要重新读取:
float gravity = 0.75;
float speed = 10;
else if (keyCode == UP|| key == 'w') {
while (speed >1 ) {
playerYPosition = playerYPosition-speed;
speed = speed * gravity;
}
gravity = -1.25;
speed = speed * gravity;
playerYPosition = playerYPosition-speed;
while (speed < -1 && speed > -10) {
playerYPosition = playerYPosition-speed;
speed = speed * gravity;
}
gravity = 0.75;
speed = 10;
float重力=0.75;
浮动速度=10;
else if(keyCode==UP | | key=='w'){
而(速度>1){
PlayerPosition=PlayerPosition速度;
速度=速度*重力;
}
重力=-1.25;
速度=速度*重力;
PlayerPosition=PlayerPosition速度;
同时(速度<-1和速度>-10){
PlayerPosition=PlayerPosition速度;
速度=速度*重力;
}
重力=0.75;
速度=10;
如果使用while循环计算Y位置,您将无法可视化跳跃模拟,因为所有数学运算都将在一帧中计算。
要进行基于物理的跳跃模拟,你需要有一个代表地面的变量(这是你的初始Y变量),而且,一旦你的玩家点击,你需要给速度变量你的全速,然后每一帧减少你的重力,同时检查每一帧,如果你还没有到达地面。
下面是我编写的一个示例,它演示了我如何保持变量名:
final float gravity = 0.5;
final float jmp_speed = 10; //the speed which the square is going to jump
float speed = 0; //the actual speed every frame
final float ystart = 280; //the initial Y position ( which also represent the Y of the ground )
float playerYPosition = ystart; //give the player the Y of the ground on start
void setup(){
size(300,300);
}
void keyPressed() {
//also check that the player is on ground before jumping
if( (keyCode == UP || key == 'w') && (playerYPosition == ystart) )
{
speed=-jmp_speed; //when the player press jump and he is on ground we give speed the max value
}
}
void draw(){
background(150);
rect(150,playerYPosition,10,10);
//this code will be executed every frame
//check if the player Y position won't hit the ground if we increment it by speed
if(playerYPosition+speed < ystart)
{
playerYPosition+=speed;
speed+=gravity; //increment speed by gravity ( will make speed value go from negative to positive slowly)
}
else
{
//if the player is gonna hit the ground the next frame
playerYPosition = ystart; // put back the player on ground
speed=0; // make the speed 0
}
}
最终浮子重力=0.5;
final float jmp_speed=10;//正方形将跳转的速度
浮点速度=0;//每帧的实际速度
最终浮动Y开始=280;//初始Y位置(也代表地面的Y)
float playerPosition=ystart;//给玩家开始时地面的Y
无效设置(){
尺寸(300300);
}
按下void键(){
//跳跃前还要检查运动员是否在地面上
如果((键代码==UP | |键==w')&&(PlayerPosition==ystart))
{
速度=-jmp_speed;//当玩家按下跳跃键时,他在地面上,我们给速度取最大值
}
}
作废提款(){
背景(150);
rect(150,playerYPosition,10,10);
//此代码将每帧执行一次
//如果我们增加速度,检查玩家的Y位置是否不会触地
if(播放位置+速度<开始)
{
PlayerPosition+=速度;
速度+=重力;//通过重力增加速度(将使速度值从负缓慢变为正)
}
其他的
{
//如果玩家在下一帧要落地
playerYPosition=ystart;//将玩家放回地面
速度=0;//使速度为0
}
}
为什么速度=速度*重力;
?自由落体时,速度以恒定的斜率线性增加。@JohnColeman速度=速度*重力;
Schoull会一直略微增加速度,因此落下的物体落得越长,落得越快。但是——这在物理上是不现实的,这正是问题所在你指的是。我推荐丹尼尔·希夫曼(Daniel Schiffman)的《代码的本质》一书,介绍如何在处理过程中处理重力引起的加速度。Youtube上有一些不错的基于代码本质的视频。加速度应该加在速度上,而不是乘以速度。
final float gravity = 0.5;
final float jmp_speed = 10; //the speed which the square is going to jump
float speed = 0; //the actual speed every frame
final float ystart = 280; //the initial Y position ( which also represent the Y of the ground )
float playerYPosition = ystart; //give the player the Y of the ground on start
void setup(){
size(300,300);
}
void keyPressed() {
//also check that the player is on ground before jumping
if( (keyCode == UP || key == 'w') && (playerYPosition == ystart) )
{
speed=-jmp_speed; //when the player press jump and he is on ground we give speed the max value
}
}
void draw(){
background(150);
rect(150,playerYPosition,10,10);
//this code will be executed every frame
//check if the player Y position won't hit the ground if we increment it by speed
if(playerYPosition+speed < ystart)
{
playerYPosition+=speed;
speed+=gravity; //increment speed by gravity ( will make speed value go from negative to positive slowly)
}
else
{
//if the player is gonna hit the ground the next frame
playerYPosition = ystart; // put back the player on ground
speed=0; // make the speed 0
}
}