Collision problems with the edges of the window
in
Programming Questions
•
1 year ago
Everytime I run into the edges while still holding down the key, the direction it was supposed to take me suddenly reverses. I know this is because it reverses velocity while the edge of the ellipse is still outside of the screen, and I've tried to move the circle in the window when it moves out, but nothing I do is working. I would appreciate any help :D.
code:
boolean jumping = false;
boolean[] pressedKeys = new boolean[256];
int radius = 50;
float xvel, yvel, yAccel, xpos, ypos;
boolean [] collision = new boolean[2];
int x = -1;
int moveUp = 'w';
int slide = 's';
int moveLeft = 'a';
int moveRight = 'd';
int jump = ' ';
void setup() {
size(800, 600);
smooth();
xpos = 150;
ypos = 500;
xvel = 0.3;
yvel = 0.3;
yAccel = 0.1;
}
void draw() {
background(204);
line(200, 400, 400, 400);
controls();
ellipse(xpos, ypos, 2*radius, 2*radius);
xWallCollision();
yWallCollision();
if (collision[0])
{
xvel = (-1)*xvel;
}
if (collision[1])
{
yvel = (-1)*yvel;
}
}
void controls()
{
if (pressedKeys[119] || pressedKeys[87])
ypos = ypos - yvel;
if (pressedKeys[83] || pressedKeys[115])
ypos = ypos + yvel;
if (pressedKeys[65] || pressedKeys[97]) {
xpos = xpos - xvel;
}
if (pressedKeys[68] || pressedKeys[100])
{
xpos = xpos + xvel;
}
if (pressedKeys[32]) //space
ypos = ypos - 1;
}
void keyPressed() {
if (key < 256)
pressedKeys[key] = true;
}
void keyReleased() {
if (key < 256)
pressedKeys[key] = false;
}
boolean yWallCollision () {
if
((height - (ypos + radius)) <= 0 || ypos - radius <= 0)
{
collision[1] = true;
}
else if
((height - (ypos + radius)) > 0 )
{
collision[1] = false;
}
return collision[1];
}
boolean xWallCollision () {
if
((width - (xpos + radius) ) <= 0 ||
xpos - radius <= 0)
{
collision[0] = true;
}
else if
((width - (xpos + radius)) > 0)
{
collision[0] = false;
}
return collision[0];
}
code:
boolean jumping = false;
boolean[] pressedKeys = new boolean[256];
int radius = 50;
float xvel, yvel, yAccel, xpos, ypos;
boolean [] collision = new boolean[2];
int x = -1;
int moveUp = 'w';
int slide = 's';
int moveLeft = 'a';
int moveRight = 'd';
int jump = ' ';
void setup() {
size(800, 600);
smooth();
xpos = 150;
ypos = 500;
xvel = 0.3;
yvel = 0.3;
yAccel = 0.1;
}
void draw() {
background(204);
line(200, 400, 400, 400);
controls();
ellipse(xpos, ypos, 2*radius, 2*radius);
xWallCollision();
yWallCollision();
if (collision[0])
{
xvel = (-1)*xvel;
}
if (collision[1])
{
yvel = (-1)*yvel;
}
}
void controls()
{
if (pressedKeys[119] || pressedKeys[87])
ypos = ypos - yvel;
if (pressedKeys[83] || pressedKeys[115])
ypos = ypos + yvel;
if (pressedKeys[65] || pressedKeys[97]) {
xpos = xpos - xvel;
}
if (pressedKeys[68] || pressedKeys[100])
{
xpos = xpos + xvel;
}
if (pressedKeys[32]) //space
ypos = ypos - 1;
}
void keyPressed() {
if (key < 256)
pressedKeys[key] = true;
}
void keyReleased() {
if (key < 256)
pressedKeys[key] = false;
}
boolean yWallCollision () {
if
((height - (ypos + radius)) <= 0 || ypos - radius <= 0)
{
collision[1] = true;
}
else if
((height - (ypos + radius)) > 0 )
{
collision[1] = false;
}
return collision[1];
}
boolean xWallCollision () {
if
((width - (xpos + radius) ) <= 0 ||
xpos - radius <= 0)
{
collision[0] = true;
}
else if
((width - (xpos + radius)) > 0)
{
collision[0] = false;
}
return collision[0];
}
1