Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  Well i have tried but cant get it to work "Prevented Collision Detection"  (Read 5605 times)
0 Members and 1 Guest are viewing this topic.
Offline VIrtueeL
« Posted 2014-11-16 08:21:40 »

ok first you might wonder what "Prevented CD" is!

"Prevented CD"
you are walking towards a wall and the wall stops the player/you from walking through it!

"Event CD"
you shoot a bullet at a wall "The Bullet" can go through the wall but you most likely remove it and make it disappear

my problem is that i never understand how to create this checks i have tried some tutorials but the collision just makes me go in to the wall then be stuck there i don't get it perfect as i want it and i cant move it back or set new player location then it makes the player sometimes just go on top or at the bottom off the block

i have tried many tutorials so if you could create a good tutorial down below that is easy to read and understand! then thanks!

please help me i just want to move on i have had this problem for over a year now i want to learn how to do it so i don't need to struggle with it anymore!

- VirtueeL

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline DavidLasry

Junior Devvie


Medals: 1
Projects: 1



« Reply #1 - Posted 2014-11-16 10:41:15 »

Currently I'm working on a game that involves such type of collision detection(the kind that prevents the player from walking through the wall). Here is a piece of my code that takes care of that:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
@Override
   public void setMonkeyPosition(float x, float y) {
      if (x+Values.Monkey_Width >= Values.SCREEN_WIDTH)
         x = Values.SCREEN_WIDTH - Values.Monkey_Width;
      else if (x <= 0)
         x = 0;
      if (y+Values.Monkey_Height >= Values.SCREEN_HEIGHT)
         y = Values.SCREEN_HEIGHT - Values.Scalar_Height;
      else if (y <= 0)
         y = 0;
      monkeyPosition.set(x+Values.Monkey_Width/2, y+Values.Monkey_Height/2);
      // Tween Engine Interpolation to Monkey
      UIHelper.tweenMonkeyMovement(monkey, x, y);
   }


Everytime the player moves, the new coordinates are checked and if they are beyond the screen, they are not defined as the new player's position.
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #2 - Posted 2014-11-16 12:36:40 »

sounds like something like ...

event :

- take previous position
- trace from previous position to current position
- test for intersection
- constraint object position according to intersection

prevented :

- take current position
- calculate future position based on direction and speed
- trace from current position to future position
- test for intersection
- constraint input (which would make an object move) to prevent object being moved beyond intersection.

or maybe :

event : collision happend, fix world
prevented : collision will happen, fix input.

both ways are good.

now, to implement that it depends on your current code. the way you setup the world, how you handle input, how to do the animations, etc. you have a few systems playing together. .. is the world full 3D or 2D .. or tilebased, or or or Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VIrtueeL
« Reply #3 - Posted 2014-11-16 13:40:12 »

want me to post my movement code? and block class? and input class?

and you might be able to help me ?

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline trollwarrior1
« Reply #4 - Posted 2014-11-16 13:45:24 »

http://www.java-gaming.org/topics/2d-collision-detection/31488/view.html
Offline VIrtueeL
« Reply #5 - Posted 2014-11-16 13:55:50 »

thanks but i dont use openGL or libGDX i use java2D

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline trollwarrior1
« Reply #6 - Posted 2014-11-16 14:01:15 »

The tutorial is not about libgx or OpenGL, its about collision detection.
Offline VIrtueeL
« Reply #7 - Posted 2014-11-16 14:02:27 »

its written in lwjgl ,opengl

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline VIrtueeL
« Reply #8 - Posted 2014-11-16 14:20:22 »

i got it to work but now there is another problem....


| = wall
[] = player

when i walk in to it, it some times stops at different places! and sometimes inside the wall!

|[]
| []
|  []
|[]
|  []
|[]

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline trollwarrior1
« Reply #9 - Posted 2014-11-16 14:27:03 »

Instead of
1  
2  
3  
4  
5  
speed = 5;
if (level.canMove(position + speed))
{
     position += speed;
}


Do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
speed = 5;

for (int i = 0; i < speed; i++)
{
     if (level.canMove(position + 1))
     {
          position += 1;
     }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VIrtueeL
« Reply #10 - Posted 2014-11-16 14:33:16 »

this is how i do it now!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
      for(Block b : PlayingState.blocks){
         if(b.isSolid()){
           
            //CHECKS
            for (int i = 0; i < speed; i++){
               if(isLeft() && PlayingState.checkCollision(xpos + 1 + PlayingState.xOffset, ypos + height + PlayingState.yOffset)
                     || PlayingState.checkCollision(xpos + 1 + PlayingState.xOffset, ypos  + PlayingState.yOffset)
                     ){
                  left = false;
               }
            }
           
         }
      }

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Tekkerue

Senior Devvie


Medals: 7
Projects: 1



« Reply #11 - Posted 2014-11-16 16:48:42 »

The method trollwarrior1 mentioned is too move the player inside the for loop by "1" if that spot is open. So if your speed is 10, and there is a wall "8" away, your player will be able to move by 7 (given that 8 will prevent the player from moving forward) within the loop and when it exits the loop you will be right next to the wall.

From what I gather of your code, it doesn't appear that you update the player's position by "1" each time through the loop. This would explain why you end up at different positions from the wall. So using the previous example, if your speed is 10 and there is a wall 8 away, when you check each position up to 10 and then only set motion to "false" if there there is a wall at any point in that range (which 8 would be) then you will immediately stop moving and remain 8 away from the wall.

I assume you check the "left" flag somewhere else and then update the position according to the flag? If so, then you could remove the flag and update the position right there inside of the loop. Then you could update the position by "1" instead of only being able to update position by the value of speed.
Offline VIrtueeL
« Reply #12 - Posted 2014-11-16 17:09:32 »

could you give me an example? once i learn this i wont need to learn it agine! =D

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline lcass
« Reply #13 - Posted 2014-11-16 17:32:11 »

It's fairly simple , instead of checking the current position of the object you check the 4 relative (or however many planes that the object may touch) planes. so instead of if(checkcollision(position.x,position.y)) instead do if(checkcollision(position.x+1,position.y+1))
Offline Tekkerue

Senior Devvie


Medals: 7
Projects: 1



« Reply #14 - Posted 2014-11-16 17:38:52 »

It's kind of hard to give an example that works specifically in your code since you are doing other things outside of your short snippet.

Elsewhere in your code, you determine whether you are moving left and set the "left" flag, correct? If so, then instead of setting a flag that you are moving left, put the check loop there to see if you can move left and move left if you can.

There maybe a better way to do this but just a quick idea:

Also, I’m not sure what the PlayingState.offset does exactly, so I’m removing it for now just to illustrate the idea. Wink

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
if (***LEFT key press***){
   for (int i = 0; i < speed; i++){
      // CHECK
      if(PlayingState.checkCollision(xpos - 1, ypos + height) || PlayingState.checkCollision(xpos - 1, ypos)){
         break;
      }
      // MOVE
      xpos -= 1;
   }
}
if (***RIGHT key press***){
   for (int i = 0; i < speed; i++){
      // CHECK
      if(PlayingState.checkCollision(xpos + 1, ypos + height) || PlayingState.checkCollision(xpos + 1, ypos)){
         break;
      }
      // MOVE
      xpos += 1;
   }
}


That performs your collision check and if there is no collision it updates the position by one and keeps going until it moves the position by "speed". When there is a collision it breaks out of the loop and doesn't update the position. You would do the same thing for vertical movement.
Offline VIrtueeL
« Reply #15 - Posted 2014-11-16 18:12:14 »

fixed the teleport it was that i was checking all blocks at the same time * speed

that made me teleport to a wall =/ perfect!
but i dont want to teleport
xD

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Tekkerue

Senior Devvie


Medals: 7
Projects: 1



« Reply #16 - Posted 2014-11-17 15:49:08 »

How about stopping right next to the wall? Were you able to get that working also?
Offline Springrbua
« Reply #17 - Posted 2014-11-18 06:58:06 »

I did not read the whole page, but i just want to throw in something: 
I guess the terms "Prevented Collision Detection" and "Event Collision Detection" are not the rigth one. 
They are both some kind of Collision Response, the Detection itself should be the same and occures before the 2 described things happen. 

So basicly Collision Detection is the detection, of whether 2 objects touch/hit/overlap each other or not. 
The Collision Response then describes what the objects should do. 
While the first kind of collision detection should move the moving entity back to the last posible position, 
the second one should damage one or both touching entities.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (583 views)
2019-09-04 15:33:17

hadezbladez (5517 views)
2018-11-16 13:46:03

hadezbladez (2403 views)
2018-11-16 13:41:33

hadezbladez (5779 views)
2018-11-16 13:35:35

hadezbladez (1226 views)
2018-11-16 13:32:03

EgonOlsen (4664 views)
2018-06-10 19:43:48

EgonOlsen (5684 views)
2018-06-10 19:43:44

EgonOlsen (3199 views)
2018-06-10 19:43:20

DesertCoockie (4096 views)
2018-05-13 18:23:11

nelsongames (5116 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04: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!