Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

player collision with move ?

edited January 2018

How to make player collision with moving player ? only 2 rectangles hits and moving AWSD i dont know how to make it :/ thx to help.

Tagged:

• edited January 2018

For rectangle-rectangle collision detection, try this example:

• The easiest way to check collision between tho bodyes is the "circle collision algorithm", you set a radius for any body and check this formula for body 1 to 2 for example.. if( distance < b1.radius+b2.radius ).. if true.. collision exists!

you can find many example codes in this forum.

• Thats i want :) THX

• But why is not here each side ? it can detect only (up or down) and (left or right)

``````//if I keep moving in my current X direction, will I collide with the center rectangle?
if (bouncingRectX + bouncingRectWidth + bouncingRectSpeedX > centerRectX &&
bouncingRectX + bouncingRectSpeedX < centerRectX + centerRectWidth &&
bouncingRectY + bouncingRectHeight > centerRectY &&
bouncingRectY < centerRectY + centerRectHeight) {
bouncingRectSpeedX *= -1;
}
``````
• shouldn't you for bounce detect x and y separately and say separately

``````      bouncingRectSpeedX *= -1;
``````

OR

``````              bouncingRectSpeedY *= -1;   // Y !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
``````
• edited January 2018

Post your entire, runnable sketch, nicely formatted and tell us the significant line numbers

Did you follow

The example below is from there!!!!!!!!!!!!!

• ``````float centerRectX = 125;
float centerRectY = 100;
float centerRectWidth = 50;
float centerRectHeight = 100;

float bouncingRectX = 10;
float bouncingRectY = 10;
float bouncingRectWidth = 100;
float bouncingRectHeight = 50;
float bouncingRectSpeedX = 1;
float bouncingRectSpeedY = 2;

void setup() {
size(300, 300);
noFill();
}

void draw() {
background(64);

//draw the center rectangle
stroke(255);
rect(centerRectX, centerRectY, centerRectWidth, centerRectHeight);

//if I keep moving in my current X direction, will I collide with the center rectangle?
if (bouncingRectX + bouncingRectWidth + bouncingRectSpeedX > centerRectX &&
bouncingRectX + bouncingRectSpeedX < centerRectX + centerRectWidth &&
bouncingRectY + bouncingRectHeight > centerRectY &&
bouncingRectY < centerRectY + centerRectHeight) {
bouncingRectSpeedX *= -1;
}
//bounce off left and right edges of screen
else if(bouncingRectX < 0 || bouncingRectX + bouncingRectWidth > width){
bouncingRectSpeedX *= -1;
}

//if I keep moving in my current Y direction, will I collide with the center rectangle?
if (bouncingRectX + bouncingRectWidth > centerRectX &&
bouncingRectX < centerRectX + centerRectWidth &&
bouncingRectY + bouncingRectHeight + bouncingRectSpeedY > centerRectY &&
bouncingRectY + bouncingRectSpeedY < centerRectY + centerRectHeight) {
bouncingRectSpeedY *= -1;
}
//bounce off top and bottom edges of screen
else if(bouncingRectY < 0 || bouncingRectY + bouncingRectHeight > height){
bouncingRectSpeedY *= -1;
}

bouncingRectX += bouncingRectSpeedX;
bouncingRectY += bouncingRectSpeedY;

//draw the bouncing rectangle
rect(bouncingRectX, bouncingRectY, bouncingRectWidth, bouncingRectHeight);
}
``````
• yes, but where is hit on: left right up down ?

• it is only left and right + up and down

• i need to move WASD with player and i must have each hit

• left right up down ?

Did you run my example from the website?

It hits on all 4 sides.

When you need real help:

• Hit ctrl-t in processing please

• Post your entire, runable sketch, nicely formatted, and tell us the significant line numbers

• describe your problem in full sentences

• I want to make solid blocks and solid player, and i dont know hot to make it :/

``````ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
int left, right, up, down;
float bouncingRectX = 150;
float bouncingRectY = 150;
float bouncingRectWidth = 20;
float bouncingRectHeight = 20;
float bouncingRectSpeedX = 1;
float bouncingRectSpeedY = 1;

void setup() {
size(300, 300);

}

void draw() {
background(64);

//iterate over the obstacles
for (int i = 0; i < rectangles.size(); i++) {

//check collision for this obstacle
Rectangle rectangle = rectangles.get(i);

//check X movment bounce
if (bouncingRectX + bouncingRectWidth + bouncingRectSpeedX > rectangle.x &&
bouncingRectX + bouncingRectSpeedX < rectangle.x + rectangle.rectWidth &&
bouncingRectY + bouncingRectHeight > rectangle.y &&
bouncingRectY < rectangle.y + rectangle.rectHeight) {

bouncingRectSpeedX *= -1;
}

//check Y movement bounce
if (bouncingRectX + bouncingRectWidth> rectangle.x &&
bouncingRectX< rectangle.x + rectangle.rectWidth &&
bouncingRectY + bouncingRectHeight + bouncingRectSpeedY > rectangle.y &&
bouncingRectY + bouncingRectSpeedY < rectangle.y + rectangle.rectHeight) {

bouncingRectSpeedY *= -1;
}

//draw the obstacle rectangle
fill(255, 0, 0);
rect(rectangle.x, rectangle.y, rectangle.rectWidth, rectangle.rectHeight);
}

bouncingRectX += (right-left)*bouncingRectSpeedX;
bouncingRectY += (down-up)*bouncingRectSpeedY;

//draw the bouncing rectangle
fill(0, 255, 0);
rect(bouncingRectX, bouncingRectY, bouncingRectWidth, bouncingRectHeight);
}

class Rectangle {
float x;
float y;
float rectWidth;
float rectHeight;

public Rectangle(float x, float y, float rectWidth, float rectHeight) {
this.x = x;
this.y = y;
this.rectWidth = rectWidth;
this.rectHeight = rectHeight;
}
}

void keyPressed() {
if (key == 'a') {
left = 1;
}
if (key == 'w') {
up = 1;
}
if (key == 's') {
down = 1;
}
if (key == 'd') {
right = 1;
}
}

void keyReleased() {
if (key == 'a') {
left = 0;
}
if (key == 'w') {
up = 0;
}
if (key == 's') {
down = 0;
}
if (key == 'd') {
right = 0;
}
}
``````
• describe your problem in full sentences

Well. You haven't.

In the example, the block (hero) is self moving. Therefore it says : `bouncingRectSpeedX *= -1;`

In your program the block (hero) is NOT self moving. Therefore you could replace `bouncingRectSpeedX *= -1;` by `left = 0;up = 0; down = 0; right = 0;` to make it stop.

that's the logical behavior, when you run into a wall, you stop.

• How do you know when a player will hit each of the four walls?

• Well, again, you are asking one very imprecise question without explaining what is happening now, what you want to happen instead and what your thoughts are on the matter

How do you know when a player will hit each of the four walls?

these are the two big if-clauses

//check X movment bounce and //check Y movment bounce

That's the way to know it. Did you mean that? We can't tell.

it doesn't matter, you can just always (in both cases) say left = 0;up = 0; down = 0; right = 0; so it does stop no matter where it went before

• I simply want the player to go through the blocks and to move the keys to the directions and I'm mainly concerned with the collision.

• not go

• Really, what are you talking about? The green player is not going through the red obstacles.

What. do. you. mean?

• -_- but the directory changed !

• PLS test the code yourself

• edited January 2018

I tested the code several times now

as I said

Therefore you could replace bouncingRectSpeedX *= -1; by left = 0;up = 0; down = 0; right = 0; to make it stop.

that's the logical behavior, when you run into a wall, you stop.

Did you do that? For bouncingRectSpeedY as well?

• So your issue was NOT player to go through the blocks but the directory changed !

But you never said so at first. So do you realize how imprecise your description of your problems was?