# collision in applet game

• November 4th, 2012, 10:45 AM
hwoarang69
collision in applet game
i have a player and platform. i want to test if player hit jump from bottom of platform thant it doesnt go though it.
so player should bonuce right back on ground.

i comment code line by line but if can understant statnd let me know. and ill explain more.

variable info:
playerX = player x postion
playerY = player y postion
playerH = player height
playerW = player width
x = platform x postion
y = platform y postion
width = platform width
height = platform height
p.setY() = set player y postion

Code :

```if(playerX + playerW >= x && playerX <= x + width) { if(playerY+playerH >= y && playerY <= y+height) //basic collsion between player and platform { if (playerX <= x && playerY+playerH >= y) //player on left - cant go though platform { p.setX(x - playerW); //stop player from going right } else if(playerX >= x && playerY+playerH >= y) //player on right -cant go thought it { p.setX(x + width); //stop player from going left } else if(playerY+playerH <= y) //player on top -cant go though it { p.setJUMP(false); p.setY(y - playerH); //stop player from going down } else if(playerY <= y+height) //player on bottom-cant go though it { //stop player from going down p.setY(y + playerH); } } }```

if player jump from bottom of platform than i want to go inside this if statment.
Code :

``` else if(playerY <= y+height) //player on top -bottomgo though it p.setY(y + playerH);```

1st problem is that if player is bottom of platform than it go inside this if statment. which is wrong
Code :

` else if(playerX >= x && playerY+playerH >= y) //player on right -cant go thought it`

2nd problem is logic question. how can i make my player bounce back on ground. here is what i am thinking
p.setY(playerY+1); //but it go though platform
• November 4th, 2012, 03:34 PM
jps
Re: collision in applet game
I have explained to you before:
Quote:

variable info:
playerX = player x postion
playerY = player y postion
playerH = player height
playerW = player width
..that all of those variables are meaningless to us unless we know where x and y are in relation to the player. Top left corner, center by center, what? You have not answered this question once in the many times I have asked it. Without knowing where on the player it is, I can not tell you how to determine when you hit a platform.

Reposting the same question over and over with the same code snippet has also been a waste of time, both yours and ours. You have also been asked for a short program that shows the problem. These snippets do not compile and run on their own, and the missing parts are also involved with what you are trying to do here.

To your 2nd problem, I explained in great detail how to use gravity. This is the same thing as jumping down instead of jumping up. When the head hits a ceiling, set the vertical speed to zero to immediately stop the player's up movement, and then allow gravity to pull the player back down, or give the player a bounce-off speed by making the vertical speed the negative of it's previous value. For example if jumping is -10, then hitting your head on the ceiling and bouncing off the negative of -10 is 10. So set vertical speed to 10 to bounce off or 0 to stop and gravity fall.
• November 4th, 2012, 03:34 PM
jps
Re: collision in applet game
I have explained to you before:
Quote:

variable info:
playerX = player x postion
playerY = player y postion
playerH = player height
playerW = player width
..that all of those variables are meaningless to us unless we know where x and y are in relation to the player. Top left corner, center by center, what? You have not answered this question once in the many times I have asked it. Without knowing where on the player it is, I can not tell you how to determine when you hit a platform.

Reposting the same question over and over with the same code snippet has also been a waste of time, both yours and ours. You have also been asked for a short program that shows the problem. These snippets do not compile and run on their own, and the missing parts are also involved with what you are trying to do here.

To your 2nd problem, I explained in great detail how to use gravity. This is the same thing as jumping down instead of jumping up. When the head hits a ceiling, set the vertical speed to zero to immediately stop the player's up movement, and then allow gravity to pull the player back down, or give the player a bounce-off speed by making the vertical speed the negative of it's previous value. For example if jumping is -10, then hitting your head on the ceiling and bouncing off the negative of -10 is 10. So set vertical speed to 10 to bounce off or 0 to stop and gravity fall.
• November 4th, 2012, 09:01 PM
hwoarang69
Re: collision in applet game
Quote:

info:
playerX = player x postion
playerY = player y postion
playerH = player height
playerW = player width
these are current player postion. it doesnt matter what is player x or y is. playerX will just get the current x postion. if player x is 200. than playerX = 200. if player x is 100000. than playerX = 100000. and so on.

by doing the code at bottom. we already know where the player is. player has to be touching the platform. but we are not sure if player is touching the platform from right,left,top, or bottom
Quote:

if(playerX + playerW >= x && playerX <= x + width)
{
if(playerY+playerH >= y && playerY <= y+height) //basic collsion between player and platform
thats why i put more if statment in side, to test where is player is touching the player from.

les say if player is touching the platform from right. than that mean player is on left of platform and it will go inside this statment.
Quote:

if (playerX <= x && playerY+playerH >= y)
les say if player is touching the platform from left. that mean player is on right side of platform. so it will go inside thsi if statment.
Quote:

else if(playerX >= x && playerY+playerH >= y)
les say if player is touch the platform from top. so it will go inside this ifstatment.
else if(playerY+playerH <= y)

at this point all the code work fine. the problem is when player is touching the platform from bottom.
Quote:

else if(playerY <= y+height)
i am checking if player head if touching the platform bottom point.
the reason this if statment doesnt work bc if never come to this. if player touches the platform from bottom it go inside this ifstatment.
Quote:

else if(playerX >= x && playerY+playerH >= y)
-----------------------------------------------------------------

2nd part got it to working. just need help with 1st part.
• November 4th, 2012, 09:25 PM
jps
Re: collision in applet game
Quote:

these are current player postion. it doesnt matter what is player x or y is. playerX will just get the current x postion. if player x is 200. than playerX = 200. if player x is 100000. than playerX = 100000. and so on.
then if playerY = 100000 is the head also at 100000 or are the feet at 100000? or the waist? Or is the entire player 1 pixel tall? You see in order to determine where the head is, you have to know where the x,y is in relation to the player, not the world.

Quote:

by doing the code at bottom. we already know where the player is. player has to be touching the platform. but we are not sure if player is touching the platform from right,left,top, or bottom
This makes no sense at all. What code at the bottom of what? ...and once you identify the code, what do you mean to say about it?

The rest of your post seems to be incoherent rambling about how you know what you are doing but it does not work. Snippets and cutouts here and there where it is almost impossible to follow your thought process.
You will need to post a SSCCE so we all have the same starting point.

Quote:

Yes. I still would like to know where your player x,y is in relation to the player. I would still like to see the SSCCE you have been asked for about this problem in every thread you have created so far.
• November 4th, 2012, 10:39 PM
hwoarang69
Re: collision in applet game
Quote:

then if playerY = 100000 is the head also at 100000 or are the feet at 100000? or the waist? Or is the entire player 1 pixel tall? You see in order to determine where the head is, you have to know where the x,y is in relation to the player, not the world.

...no, playerY is the head. That why i am doing playerY+playerH. playerY+playerH are the feet. if playerY is 20000000, than thats where the head is. and playerY+playerH is where the feet are. but let just say playerX is 100. and playerY is 500, platform x = 110, platform height = 20.

Quote:

This makes no sense at all. What code at the bottom of what?
....not sure if i follow. "this code at bottom". i meant the code at bottom of that line. -_-

Quote:

and once you identify the code, what do you mean to say about it?
... i dont understant what you mean by this. i am explainng there that i am setting up a collsion between player and platform. not sure what more information you need.

Quote:

The rest of your post seems to be incoherent rambling about how you know what you are doing but it does not work
yes i am rambling and trying to explain what i am doing.

-----------------------------------------------------------------------------------------------------------
any way les please fouse on the problem here.

the problem is that if player touch the platform at bottom than i want it to go inside this if statment
Quote:

else if(playerY <= y+height)
but it is going in this if statment.
Quote:

else if(playerX >= x && playerY+playerH >= y)
i already know why its doing it but i dont now how to fix this problem.
if you dont see why its doing it, here is why:
if player at bottom platform so that means playerY has to be bigger than platform Y + platform height. that why it going inside the 2nd if statment and not in last ifstatment.
• November 5th, 2012, 03:19 PM
jps
Re: collision in applet game
Quote:

playerY is the head. That why i am doing playerY+playerH.
Then that is why the head goes into the ceiling. You are tracking the player's position by the feet. You need to bump the player down when the head hits the platform, and up when the feet hit the platform. That is how you know if the player landed on top or bumped his head from below.

playerY = top of player's head
playerY + playerY = bottom of player's feet

playerX = left edge of player
playerX + playerW = right edge of player

platformY = top of platform surface
platformY + platformH = bottom of platform surface & location of ceiling to be hit from beneath

platformX = left edge of platform
platformX + platformW = right edge of platform

if(platform bounding box contains the player's head position) { player hit head on platform }
if(platform bounding box contains the player's feet position) { player landed on platform }
• November 6th, 2012, 08:03 AM
hwoarang69
Re: collision in applet game
i figure it out. all i had to do was check if player jump is false in 3rd if statement.

thanks anyway