使用 PVectors 使速度恒定
using PVectors to make speed constant
我正在处理 Processing 中的一个学校项目,我 运行 遇到了一些问题。我有一个 bulletList
数组,它在我的 draw()
class 中循环,其中 Bullet
对象在按下 space 栏时初始化。
if(key==' '){
float currentPlayerX=playerX;
float currentPlayerY=playerY;
float currentMouseX=mouseX;
float currentMouseY=mouseY;
bullets.add(new
Bullet(currentPlayerX,currentPlayerY,currentMouseX,currentMouseY));
}
//in the draw method()
for(Bullet b: bullets){
b.shoot();
}
class Bullet{
float bulletX;
float bulletY;
float rise;
float run;
Bullet(float pX, float pY,float mX,float mY){
//I get the slope from the mouse position when the bullet was instantiated
to the bullet position (which starts at the player position and is
incremented out by the slope as shoot() is called
rise=(mY-pY)/12;
run=(mX-pX)/12;
bulletX=pX;
bulletY=pY;
}
void shoot(){
fill(255,0,0);
rect(bulletX,bulletY,7,7);
bulletX+=run;
bulletY+=rise;
}
}
本质上,Bullet
的轨迹是在第一次创建bullet
对象时确定的,最终目标是:将鼠标移动到想要的方向,按下space酒吧,看着子弹走。问题是速度永远不会恒定。当鼠标远离Player
物体时,速度还可以,但随着鼠标靠近rise
和run
变小,Bullet
速度变真的很小。实际上,我最终弄乱了 rise
和 run
浮点数,并将它们除以 12,因为在它们的初始值下 Bullet
是不可见的。
我听说 PVectors
是提高游戏速度的好方法,但我们从未在我的 class 中介绍过它们,我真的不知道从哪里开始。我也试过除以和乘以一个常数来尝试获得恒定的速度,但我基本上是在黑暗中拍摄。非常感谢任何帮助,如果您对我的代码的工作方式有任何疑问,请询问;我很乐意澄清。
你要找的是一些基本的三角函数。基本上我会 break your problem down into smaller steps:
第一步:获取子弹源与鼠标的夹角。 Google "get angle between two points" 大量资源。
第二步: 将角度转换为 单位向量,这是一个 x/y 对,您可以将其视为一个标题。 Google "convert angle to unit vector" 大量资源。
第三步:将该单位向量的那些 x 和 y 值乘以某个速度以获得每帧应移动子弹的量。
PVector
class 确实提供了其中的一些功能。查看 the reference 以获取有用功能的列表。
如果您仍然遇到困难,请将您的问题缩小到 MCVE。这可以像跟随鼠标的一个圆圈一样简单。祝你好运!
除了 Kevin 的回答之外,我还想向您推荐一些有用的在线资源:
- Vector magnitude & normalization on Khan Academy
- Daniel Shiffman's Nature of Code chapter on Vectors
无论您是否选择使用 PVector,这些都可以帮助您规范化然后将矢量缩放到设定的速度。
我正在处理 Processing 中的一个学校项目,我 运行 遇到了一些问题。我有一个 bulletList
数组,它在我的 draw()
class 中循环,其中 Bullet
对象在按下 space 栏时初始化。
if(key==' '){
float currentPlayerX=playerX;
float currentPlayerY=playerY;
float currentMouseX=mouseX;
float currentMouseY=mouseY;
bullets.add(new
Bullet(currentPlayerX,currentPlayerY,currentMouseX,currentMouseY));
}
//in the draw method()
for(Bullet b: bullets){
b.shoot();
}
class Bullet{
float bulletX;
float bulletY;
float rise;
float run;
Bullet(float pX, float pY,float mX,float mY){
//I get the slope from the mouse position when the bullet was instantiated
to the bullet position (which starts at the player position and is
incremented out by the slope as shoot() is called
rise=(mY-pY)/12;
run=(mX-pX)/12;
bulletX=pX;
bulletY=pY;
}
void shoot(){
fill(255,0,0);
rect(bulletX,bulletY,7,7);
bulletX+=run;
bulletY+=rise;
}
}
本质上,Bullet
的轨迹是在第一次创建bullet
对象时确定的,最终目标是:将鼠标移动到想要的方向,按下space酒吧,看着子弹走。问题是速度永远不会恒定。当鼠标远离Player
物体时,速度还可以,但随着鼠标靠近rise
和run
变小,Bullet
速度变真的很小。实际上,我最终弄乱了 rise
和 run
浮点数,并将它们除以 12,因为在它们的初始值下 Bullet
是不可见的。
我听说 PVectors
是提高游戏速度的好方法,但我们从未在我的 class 中介绍过它们,我真的不知道从哪里开始。我也试过除以和乘以一个常数来尝试获得恒定的速度,但我基本上是在黑暗中拍摄。非常感谢任何帮助,如果您对我的代码的工作方式有任何疑问,请询问;我很乐意澄清。
你要找的是一些基本的三角函数。基本上我会 break your problem down into smaller steps:
第一步:获取子弹源与鼠标的夹角。 Google "get angle between two points" 大量资源。
第二步: 将角度转换为 单位向量,这是一个 x/y 对,您可以将其视为一个标题。 Google "convert angle to unit vector" 大量资源。
第三步:将该单位向量的那些 x 和 y 值乘以某个速度以获得每帧应移动子弹的量。
PVector
class 确实提供了其中的一些功能。查看 the reference 以获取有用功能的列表。
如果您仍然遇到困难,请将您的问题缩小到 MCVE。这可以像跟随鼠标的一个圆圈一样简单。祝你好运!
除了 Kevin 的回答之外,我还想向您推荐一些有用的在线资源:
- Vector magnitude & normalization on Khan Academy
- Daniel Shiffman's Nature of Code chapter on Vectors
无论您是否选择使用 PVector,这些都可以帮助您规范化然后将矢量缩放到设定的速度。