Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Collision Detection  (Read 832 times)
0 Members and 1 Guest are viewing this topic.
Offline Mikelmao

Senior Newbie





« Posted 2011-03-15 20:30:02 »

Hey everyone, Im a little new to java and im having a problem with my collision detection.

So let me explain my problem. When a player presses the up key the ingame character (wich is a rectangle atm) increases its Y Coords by 100 and returns to 0 after hitting 100. Iv added some collision detection when comming from the right or left. if the char come's from left -> right it will substract 1 from its X Coords. Same thing when the char come's from right -> left except increasing the X Coords upon collision.

When the char is comming down from the jump his Y coords increases, when he the char collides while comming down it will decrease its y coord by 1 every attempt of moving down. At this point the char is ontop of the rectangle wich it is colliding with and 35pixels above ground level. How can i get the char back down to ground level?

I tried many diffrent ways, one of them was detecting howmany pixels the char was above ground level, and every 5ms making it go down. But the problem with that is that it wont let me go up anymore because it keeps pusing me down, found that out the hard way ^^.

This is my code:

Check collision method: call's the method every 5 ms.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void checkCollision() {
      Rectangle r = new Rectangle(rx, ry, 50, 50);
     
      if(p.getBounds().intersects(r)) {
         if(p.moveUp == true)
            p.changeY(1);
         if(p.jumping == true)
            p.changeY(-1);
         if(p.moveLeft == true)
            p.changeX(1);
         if(p.moveRight == true)
            p.changeX(-1);
         p.colliding = true;
      } else {
         p.colliding = false;
      }
   }


My character jumping method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public void jump() {
      if(jumpMovement < 100 && inJump == true) {
         jumpMovement++;
         changeY(-1);
      } else if(jumpMovement > 0 && inJump == false) {
         jumpMovement--;
         changeY(1);
      } else {
         jumping = false;
      }
      if(jumpMovement == 100) {
         inJump = false;
      }
   }


Thanks in advance Smiley

Also: Im sorry if this isnt enough information but im not really sure how to describe my problem... If anything is unclear do post so, thanks Smiley

[EDIT] While im at it, whats the best way to calculate wich side its colliding on? above, left, right, or from under?
Offline aazimon
« Reply #1 - Posted 2011-03-15 21:37:28 »

If you have a Tile map, you can check the character's location in the map, where he is moving into and see if there is something there (like the ground). First convert the character's pixel location on the display into a grid location. If the character is at 120x45 and the grid is 16x16, he would be in grid 7 x 2. Each update you check if the grid he is moving into is the ground. You will check grid 7 x 3. Once you hit the ground, you set the characters pixel location based on the grid above the ground. If the ground is at 7 x 4, the character would be standing at 7 x 3. (Assuming the character fills a 16 x 16 grid).
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-03-15 22:08:50 »

It sounds like you have gravity in this game.
Each call to update should subtract gravity from your y velocity, then add the y velocity to the y position. When you jump, you set the velocity to a positive value so your y should decrease (aka, go up on the screen) and each time you update, the y velocity gets smaller and becomes a negative number and your y should increase (aka, go down on the screen).
What I could understand was that your character jumped, collided with something and you want it to go back down? If so then when it collides, all you need to do is set the y velocity to 0 so gravity would be subtracted from it and your player would move down.

Also, there is no need for:
1  
2  
if(p.moveUp == true)
etc....

you can just ommit the "== true" part.

Hope that helps Cheesy

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!