Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (593)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
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 982 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.
public void checkCollision() {
      Rectangle r = new Rectangle(rx, ry, 50, 50);
      if(p.getBounds().intersects(r)) {
         if(p.moveUp == true)
         if(p.jumping == true)
         if(p.moveLeft == true)
         if(p.moveRight == true)
         p.colliding = true;
      } else {
         p.colliding = false;

My character jumping method:
public void jump() {
      if(jumpMovement < 100 && inJump == true) {
      } else if(jumpMovement > 0 && inJump == false) {
      } 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: 388
Projects: 3
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:
if(p.moveUp == true)

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.

deepthought (31 views)
2015-06-30 15:39:44

deepthought (34 views)
2015-06-30 15:39:09

deepthought (45 views)
2015-06-30 15:36:52

Za\'Anzabar (21 views)
2015-06-29 05:44:54

TritonDreyja (36 views)
2015-06-24 17:10:40

CopyableCougar4 (35 views)
2015-06-23 00:34:45

BurntPizza (39 views)
2015-06-21 20:36:46

cookiecompiler (82 views)
2015-06-11 15:42:53

cookiecompiler (45 views)
2015-06-11 15:41:14

NegativeZero (75 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 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‑
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!