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 6 of 6

Thread: need help with basc Collisions

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

    Default need help with basc Collisions

    so i have a player and i want to code collision, so player dont fall down ground.

    i have 3 classes
    Main.java
    Player.java
    Ground.java


    in my Ground.java. i want to set collions between player and ground.
    this code doesnt work, i am not sure what i am doing wrong. if u need more information let me know. i didnt wanted to past 3 pages of code.

    public void update(Main m, Player p)
    	{
    		int playerX = p.getX();    //get player x position
    		int playerY = p.getY();    //get player y poistion
     
                   //y = ground y poistion  | x = ground x poistion
                   //height = ground height | width = ground width
     
    		if(playerY > y && playerY < y + height)
    		{
    			if(playerX > x && playerX < x + width)
    			{
    				int newDY = p.getDY() * - 1; 
    				p.setY(y);
    				p.setDY(newDY);
    			}
    		}
    	}


  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: need help with basc Collisions

    What do you mean the code does not work? What does not work? What is the error?

    Some ideas to think about, and if you still have a question try to fill in as many details about what the code does and what you want it to do instead.
    Add some printlns and see what you find.

    Is Player.getY the center point of the player? Foot level? Eye level? How does that compare to ground y?

    if(playerY > y && playerY < y + height) {
    Is the conditional block being executed or skipped? Do these variables have correct values? What does sysout show?
    Same question for the next conditional block.
    Should x conditional be contained within the y conditional? What effect might this have on player movement in terms of walking across a flat ground?

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

    Default Re: need help with basc Collisions

    let me try again.

    so i have a player, and i have platform's which is i am calling ground.
    i want it so that my player can jump on top of platform's and if it hit the bottom of platform it should bounce back down and not go through it.

    1st i want to set up collision point bottom of player feet, so it can jump top of platform.
    2nd i want to set up collision point top of player head, so it does'nt go though platform when u jump from bottom of platform.

    i am getting player x and y poition here
    int playerX = p.getX(); //get player x position
    int playerY = p.getY(); //get player y poistion


    here i am seting up collion so that playerY and platform height.(y+height)
    if(playerY > y && playerY < y + height)

    same thing here but with X postion
    if(playerX > x && playerX < x + width)

    here i am setting player's y and dy
    int newDY = p.getDY() * - 1;
    p.setY(y);
    p.setDY(newDY);


    i am not getting any error but the problem is that if i jump on top of my platform, my player go though it. but it should stand top of platform.
    and if it jump from bottom of platform, than his head should hit the platform and bonce back down.

    also in ur answer try to explain as much as u can but iam new to game programming,

    Thanks

  4. #4
    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: need help with basc Collisions

    1st i want to set up collision point bottom of player feet, so it can jump top of platform.
    2nd i want to set up collision point top of player head, so it does'nt go though platform when u jump from bottom of platform.
    Great thinking. A list of steps to take (preferably in order) to achieve the goal. Keep going with the list.



    i am getting player x and y poition here
    int playerX = p.getX(); //get player x position
    int playerY = p.getY(); //get player y poistion
    That may get a position, but the thing to think about is where that position is relative to the player. Is it at head level? foot level? top left corner of the bounding box? center by center of the bounding box? Once you determine where that point is, you can determine how far to "float" that point above floors/platforms to keep the feet walking on the bottom. Then you can determine how far to keep that point sunk under ceiling objects to keep the head from going up into things. Determine where that point is in terms of the player and work out some way to keep track of the level of the bottom of the feet and the top of the head.



    I would suggest a large number of printlns to see what is happening as the code runs.

  5. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 22nd, 2012)

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

    Default Re: need help with basc Collisions

    to get top of platform i would have to do

    if player_BOTTOM_LEFT_Y > platform_TOP_LEFT_Y && player_BOTTOM_RIGHT_Y < platform_top_left + platform_height
    if player_BOTTOM_RIGHT_X > platform_TOP_LEFT_X && player_BOTTOM_RIGHT_X < platform_top_left + platform_width

  7. #6
    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: need help with basc Collisions

    to get top of platform i would have to do...
    Or, since player_BOTTOM_LEFT_Y should always equal player_BOTTOM_RIGHT_Y:
    if( playerFootHeight > groundHeight ) {
    player.falling = true;
    }

    So picture the bounding box around the player. Rather than visualizing the 4 corners of the box, as player_BOTTOM_LEFT and player_BOTTOM_RIGHT, see the edges of the rectangle.
    The top of the rectangle is the upper limit for the height.
    The bottom of the rectangle is the lower limit for the height.
    The right of the rectangle is the leading edge (for example)
    The left of the rectangle is the trailing edge.
    So you can see two points hold the y value for the feet height, or bottom of the rectangle. It simply does not matter which point you get the y value from, it is the same value. The test only needs to be done once.

    Say the player is in a jump, and attempting to land on a platform. The requirements of landing on the platform would be something like:
    if player feet are higher than platform top
    if player right edge is past platform left edge
    if player left edge is NOT past platform right edge
    player is now over top of the platform and falling...

    while player stays between edges of platform
    if player feet height == platform top
    player has landed on platform successfully

    There are many different ways to write code to detect the conditions, some better than others as is always the case. (depending on the specifics)
    So in my left-to-right jump, needing to land on an edge, the first important point to consider is the (player_BOTTOM_RIGHT_X, player_BOTTOM_RIGHT_Y) as this gives the leading edge of the player and the foot height. From that point we can detect if we caught the edge of a platform with the tip of our toes, or by a good safe landing. With just this single point, as long as that point does not go off the edge of the platform, the player can not fall off and no other point currently matters. If this point passes the left side of the platform, the player fell off the left side of the platform. If this point passes the right side of the platform, now we have to look at the bottom left corner point of the player. Only when this point goes past the right side of the platform, the player falls off the right side.

    I feel like I am rambling on.... hope it helps.

  8. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 22nd, 2012)

Similar Threads

  1. hashing / collisions
    By Herah in forum Java Theory & Questions
    Replies: 1
    Last Post: November 29th, 2011, 11:16 PM
  2. Collisions... working but not.
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 24th, 2011, 08:05 PM
  3. Particle collisions...
    By Macattack1459 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 09:44 PM