Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: collision in applet game

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default 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


    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.
       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
     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


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: collision in applet game

    I have explained to you before:
    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.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: collision in applet game

    I have explained to you before:
    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.

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: collision in applet game

    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
    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.
    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.
    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.
    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.
    else if(playerX >= x && playerY+playerH >= y)
    let me know if you need more information
    -----------------------------------------------------------------

    2nd part got it to working. just need help with 1st part.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: collision in applet game

    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.

    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.



    let me know if you need more information
    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.

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: collision in applet game

    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.


    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. -_-


    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.



    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
    else if(playerY <= y+height)
    but it is going in this if statment.
    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.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: collision in applet game

    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 }

  8. #8
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. [SOLVED] Java applet game
    By qadeerhussain007 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 9th, 2012, 08:46 AM
  2. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 05:11 PM
  3. Help with game applet
    By skerridge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 02:24 PM
  4. How to find collision between balls in Applet animation
    By AJAXx195 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 4th, 2011, 02:01 PM
  5. Need help with collision in a game!
    By Skyhigh32 in forum Java Theory & Questions
    Replies: 5
    Last Post: May 18th, 2011, 01:12 AM