如何强制处理以正确处理运动的负值?
How to force processing to properly deal a negative value for movement?
我正在为学校处理一个 "golf" 游戏,我可以让球本身移动,但它只能在向上和向左方向上正确移动。我所说的正确是指它只施加摩擦力并移动适当的量。除了向上和向左之外,它在每个方向上的移动都非常短且突然。
我已将我的代码附加给任何可以提供帮助的人,并且我已尽我所能对其进行评论,因为我知道我的思维过程在这方面可能不太好。
final float MIN_SPEED = 0.5; //Minimum speed the ball can be travelling
final float FRICTION = 0.98;
final float BALL_SIZE = 14.0;
float holeX;
float holeY;
float ballX = 250;
float ballY = 250;
float ballSpeedX;
float ballSpeedY;
float ballDistanceX;
float ballDistanceY;
float xVelocity = 0;
float yVelocity = 0;
boolean isHole = false;
boolean move = false;
void setup(){
size(500,500);
}
/* Function to set initial X & Y Velocities as well as move to true when the mouse is clicked and released */
void mouseClicked(){
xVelocity = ballDistanceX * 0.04;
yVelocity = ballDistanceY * 0.04;
move = true;
}
void draw(){
background(0);
stroke(255);
fill(255);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // initializes the ball to the center
if(move == false){ // draws a line from the cursor to the ball when it is not moving
fill(255);
line(mouseX, mouseY, ballX, ballY);
ballDistanceX = mouseX - ballX;
ballDistanceY = mouseY - ballY;
}
else if(move == true){ //When the mouse has been clicked, moves the ball the opposite direction of where the click was
xVelocity *= FRICTION; // applies friction to slow the ball down
yVelocity *= FRICTION;
if (ballX >= 0 && ballY >= 0){
ballX -= xVelocity; // changes the position of ballX
ballY -= yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // redraws the ball in its new position
}
if (ballX >= 0 && ballY <= 0){
ballX -= xVelocity;
ballY += yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
if (ballX <= 0 && ballY >= 0){
ballX += xVelocity;
ballY -= yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
if (ballX < 0 && ballY < 0){
ballX += xVelocity;
ballY += yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
if (xVelocity < MIN_SPEED || yVelocity < MIN_SPEED) { // Once the ball slows down enough it resets move to false
move = false;
}
}
if (xVelocity < MIN_SPEED || yVelocity < MIN_SPEED) {
move = false;
}
}
如果有什么我可以做的来澄清它或让它更容易理解,请告诉我!
很好地识别了导致问题的部件。
您正在检查 if(xVelocity < MIN_SPEED || yVelocity < MIN_SPEED)
。
问题是您只检查 x 或 y 速度是否为正且在一个小阈值 (0.0 - 0.5) 内。
如果速度是你的矢量,你要检查的是矢量的大小。请务必查看 Daniel Shiffman's Vectors chapter on Nature of Code 或他关于线性代数的一些 youtube 视频。
这个想法是矢量的大小是矢量的长度。线越大,矢量越大(在您的情况下为速度),无论方向如何(正或负 x,y 分量)。它只是使用欧氏距离(毕达哥拉斯定理)
这是您的代码的调整版本,删除了 运行 不需要的变量。此外,您并不真正需要 while
循环:draw()
已经是您要在以下范围内更新的无限循环:
final float MIN_SPEED = 0.5; //Minimum speed the ball can be travelling
final float FRICTION = 0.98;
final float BALL_SIZE = 14.0;
float ballX = 250;
float ballY = 250;
float ballDistanceX;
float ballDistanceY;
float xVelocity = 0;
float yVelocity = 0;
boolean move = false;
void setup() {
size(500, 500);
}
/* Function to set initial X & Y Velocities as well as move to true when the mouse is clicked and released */
void mouseClicked() {
xVelocity = ballDistanceX * 0.04;
yVelocity = ballDistanceY * 0.04;
move = true;
}
void draw() {
background(0);
stroke(255);
fill(255);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // initializes the ball to the center
if (move == false) { // draws a line from the cursor to the ball when it is not moving
fill(255);
line(mouseX, mouseY, ballX, ballY);
ballDistanceX = mouseX - ballX;
ballDistanceY = mouseY - ballY;
} else { //When the mouse has been clicked, moves the ball the opposite direction of where the click was
xVelocity *= FRICTION; // applies friction to slow the ball down
yVelocity *= FRICTION;
ballX -= xVelocity; // changes the position of ballX by subtracting velocity
ballY -= yVelocity;
}
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // redraws the ball in its new position
float mag = sqrt(xVelocity*xVelocity + yVelocity*yVelocity);
if(mag < MIN_SPEED){
move = false;
}
}
您也可以 运行 代码如下 p5.js 草图:
const MIN_SPEED = 0.5; //Minimum speed the ball can be travelling
const FRICTION = 0.98;
const BALL_SIZE = 14.0;
var ballX = 250;
var ballY = 250;
var ballDistanceX;
var ballDistanceY;
var xVelocity = 0;
var yVelocity = 0;
var move = false;
function setup() {
createCanvas(500, 500);
}
/* Function to set initial X & Y Velocities as well as move to true when the mouse is clicked and released */
function mouseClicked() {
xVelocity = ballDistanceX * 0.04;
yVelocity = ballDistanceY * 0.04;
move = true;
}
function draw() {
background(0);
stroke(255);
fill(255);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // initializes the ball to the center
if (move == false) { // draws a line from the cursor to the ball when it is not moving
fill(255);
line(mouseX, mouseY, ballX, ballY);
ballDistanceX = mouseX - ballX;
ballDistanceY = mouseY - ballY;
} else { //When the mouse has been clicked, moves the ball the opposite direction of where the click was
xVelocity *= FRICTION; // applies friction to slow the ball down
yVelocity *= FRICTION;
ballX -= xVelocity; // changes the position of ballX by subtracting velocity
ballY -= yVelocity;
}
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // redraws the ball in its new position
var mag = sqrt(xVelocity*xVelocity + yVelocity*yVelocity);
if(mag < MIN_SPEED){
move = false;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.min.js"></script>
我正在为学校处理一个 "golf" 游戏,我可以让球本身移动,但它只能在向上和向左方向上正确移动。我所说的正确是指它只施加摩擦力并移动适当的量。除了向上和向左之外,它在每个方向上的移动都非常短且突然。
我已将我的代码附加给任何可以提供帮助的人,并且我已尽我所能对其进行评论,因为我知道我的思维过程在这方面可能不太好。
final float MIN_SPEED = 0.5; //Minimum speed the ball can be travelling
final float FRICTION = 0.98;
final float BALL_SIZE = 14.0;
float holeX;
float holeY;
float ballX = 250;
float ballY = 250;
float ballSpeedX;
float ballSpeedY;
float ballDistanceX;
float ballDistanceY;
float xVelocity = 0;
float yVelocity = 0;
boolean isHole = false;
boolean move = false;
void setup(){
size(500,500);
}
/* Function to set initial X & Y Velocities as well as move to true when the mouse is clicked and released */
void mouseClicked(){
xVelocity = ballDistanceX * 0.04;
yVelocity = ballDistanceY * 0.04;
move = true;
}
void draw(){
background(0);
stroke(255);
fill(255);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // initializes the ball to the center
if(move == false){ // draws a line from the cursor to the ball when it is not moving
fill(255);
line(mouseX, mouseY, ballX, ballY);
ballDistanceX = mouseX - ballX;
ballDistanceY = mouseY - ballY;
}
else if(move == true){ //When the mouse has been clicked, moves the ball the opposite direction of where the click was
xVelocity *= FRICTION; // applies friction to slow the ball down
yVelocity *= FRICTION;
if (ballX >= 0 && ballY >= 0){
ballX -= xVelocity; // changes the position of ballX
ballY -= yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // redraws the ball in its new position
}
if (ballX >= 0 && ballY <= 0){
ballX -= xVelocity;
ballY += yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
if (ballX <= 0 && ballY >= 0){
ballX += xVelocity;
ballY -= yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
if (ballX < 0 && ballY < 0){
ballX += xVelocity;
ballY += yVelocity;
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
if (xVelocity < MIN_SPEED || yVelocity < MIN_SPEED) { // Once the ball slows down enough it resets move to false
move = false;
}
}
if (xVelocity < MIN_SPEED || yVelocity < MIN_SPEED) {
move = false;
}
}
如果有什么我可以做的来澄清它或让它更容易理解,请告诉我!
很好地识别了导致问题的部件。
您正在检查 if(xVelocity < MIN_SPEED || yVelocity < MIN_SPEED)
。
问题是您只检查 x 或 y 速度是否为正且在一个小阈值 (0.0 - 0.5) 内。
如果速度是你的矢量,你要检查的是矢量的大小。请务必查看 Daniel Shiffman's Vectors chapter on Nature of Code 或他关于线性代数的一些 youtube 视频。 这个想法是矢量的大小是矢量的长度。线越大,矢量越大(在您的情况下为速度),无论方向如何(正或负 x,y 分量)。它只是使用欧氏距离(毕达哥拉斯定理)
这是您的代码的调整版本,删除了 运行 不需要的变量。此外,您并不真正需要 while
循环:draw()
已经是您要在以下范围内更新的无限循环:
final float MIN_SPEED = 0.5; //Minimum speed the ball can be travelling
final float FRICTION = 0.98;
final float BALL_SIZE = 14.0;
float ballX = 250;
float ballY = 250;
float ballDistanceX;
float ballDistanceY;
float xVelocity = 0;
float yVelocity = 0;
boolean move = false;
void setup() {
size(500, 500);
}
/* Function to set initial X & Y Velocities as well as move to true when the mouse is clicked and released */
void mouseClicked() {
xVelocity = ballDistanceX * 0.04;
yVelocity = ballDistanceY * 0.04;
move = true;
}
void draw() {
background(0);
stroke(255);
fill(255);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // initializes the ball to the center
if (move == false) { // draws a line from the cursor to the ball when it is not moving
fill(255);
line(mouseX, mouseY, ballX, ballY);
ballDistanceX = mouseX - ballX;
ballDistanceY = mouseY - ballY;
} else { //When the mouse has been clicked, moves the ball the opposite direction of where the click was
xVelocity *= FRICTION; // applies friction to slow the ball down
yVelocity *= FRICTION;
ballX -= xVelocity; // changes the position of ballX by subtracting velocity
ballY -= yVelocity;
}
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // redraws the ball in its new position
float mag = sqrt(xVelocity*xVelocity + yVelocity*yVelocity);
if(mag < MIN_SPEED){
move = false;
}
}
您也可以 运行 代码如下 p5.js 草图:
const MIN_SPEED = 0.5; //Minimum speed the ball can be travelling
const FRICTION = 0.98;
const BALL_SIZE = 14.0;
var ballX = 250;
var ballY = 250;
var ballDistanceX;
var ballDistanceY;
var xVelocity = 0;
var yVelocity = 0;
var move = false;
function setup() {
createCanvas(500, 500);
}
/* Function to set initial X & Y Velocities as well as move to true when the mouse is clicked and released */
function mouseClicked() {
xVelocity = ballDistanceX * 0.04;
yVelocity = ballDistanceY * 0.04;
move = true;
}
function draw() {
background(0);
stroke(255);
fill(255);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // initializes the ball to the center
if (move == false) { // draws a line from the cursor to the ball when it is not moving
fill(255);
line(mouseX, mouseY, ballX, ballY);
ballDistanceX = mouseX - ballX;
ballDistanceY = mouseY - ballY;
} else { //When the mouse has been clicked, moves the ball the opposite direction of where the click was
xVelocity *= FRICTION; // applies friction to slow the ball down
yVelocity *= FRICTION;
ballX -= xVelocity; // changes the position of ballX by subtracting velocity
ballY -= yVelocity;
}
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE); // redraws the ball in its new position
var mag = sqrt(xVelocity*xVelocity + yVelocity*yVelocity);
if(mag < MIN_SPEED){
move = false;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.min.js"></script>