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!
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;
}
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.
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
Answers
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.
For another collision detection tutorial series, see also:
Thats i want :) THX
But why is not here each side ? it can detect only (up or down) and (left or right)
shouldn't you for bounce detect x and y separately and say separately
OR
Hit ctrl-t in processing please
Post your entire, runnable sketch, nicely formatted and tell us the significant line numbers
Did you follow
http://happycoding.io/tutorials/processing/collision-detection ?
The example below is from there!!!!!!!!!!!!!
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
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 :/
Well. You haven't.
It works pretty well already.
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;
byleft = 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
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
I tested the code several times now
as I said
Did you do that? For bouncingRectSpeedY as well?
Post your entire NEW code.
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?