Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  [Solved]Entity collision  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
Offline SonicPixelation

Senior Newbie





« Posted 2013-09-04 15:20:19 »

I have been working on a entity system that works fairly well but I have run into a problem that I can't figure out. What I want is for a entity to collide with another entity on lets say the x axis, but can still move freely on the y, entity sliding on another entity. I found little on the subject and what I did find I can't get it to work.

excert of the entity class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
   public void move(float xa, float ya, int delta){
   CollisionManager(delta);
      if(canPass){
         pos.x += xa * delta;
         pos.y += ya * delta;
      }
   }

   // collision method----------------------------------------------
   // can be overridden to add new collision
   public void CollisionManager(int delta) {
      int x0 = (int)(pos.x + (vel.x * delta));
      int y0 = (int)(pos.y + (vel.y * delta));
      int x1 = (int)(pos.x + width + (vel.x * delta));
      int y1 = (int)(pos.y + height + (vel.y * delta));
     
      List<Entity> entities = world.getEntities(x0, y0, x1, y1);
      for(int i = 0; i < entities.size(); i++){
         Entity e = entities.get(i);
         if(this == e) continue;
            this.touchedBy(e);
           
            if(this.blocks(e)){
               canPass = false;
            }
         }
     
   }

   // if the player intersects a object with 4 side
   public boolean intersects(int xt0, int yt0, int xt1, int yt1) {
      int xe0 = (int) (pos.x);
      int ye0 = (int) (pos.y);
      int xe1 = (int) (pos.x + width);
      int ye1 = (int) (pos.y + height);
      return !(xe1 < xt0 || ye1 < yt0 || xe0 > xt1 || ye0 >  yt1);

   }


and the part of the world class that looks to see if a entity is colliding with another
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public List<Entity> getEntities(int x0, int y0, int x1, int y1){
      List<Entity> result = new ArrayList<>();
     
      int xt0 = (x0 >> Tile.Tile_Width / 2) - 1;
      int yt0 = (y0 >> Tile.Tile_Height / 2) - 1;
      int xt1 = (x1 >> Tile.Tile_Width / 2) + 1;
      int yt1 = (y1 >> Tile.Tile_Height / 2) + 1;
      for (int y = yt0; y <= yt1; y++) {
         for (int x = xt0; x <= xt1; x++) {
         if(x < 0 || y < 0 || x >= width || y >= height)continue;
         
         for(int i = 0; i < entities.size(); i++){
          Entity e = entities.get(i);
         if(e.intersects(x0,y0,x1,y1)) result.add(e);
         }
      }
     
     
   }
      return result;

   }



thank you
EDIT: the list entities referse to all the entities in the gameworld
Offline alesky

Junior Devvie


Medals: 3
Exp: 15 years


mmm....


« Reply #1 - Posted 2013-09-04 16:10:13 »

if i undestand what you mean

you want to move for example in y if it is not block also if x is block, right?
in this case you can modify easily your code like this


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 public void CollisionManager(int delta) {
      int x0 = (int)(pos.x + (vel.x * delta));
      int y0 = (int)(pos.y + (vel.y * delta));
      int x1 = (int)(pos.x + width + (vel.x * delta));
      int y1 = (int)(pos.y + height + (vel.y * delta));
     
      List<Entity> entities = world.getEntities(x0, y0, x1, y1);
      for(int i = 0; i < entities.size(); i++){
         Entity e = entities.get(i);
         if(this == e) continue;
            this.touchedBy(e);
           
            if(this.blocksX(e)){
               canPassX = false;
            }
            if(this.blocksY(e)){
               canPassY = false;
            }
         }
     
   }



and in the move code function

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
 public void move(float xa, float ya, int delta){
   CollisionManager(delta);
      if(canPassX){
         pos.x += xa * delta;
      }
      if(canPassY){
         pos.y += ya * delta;
      }

   }


that mean that you have to modify your function block and split in 2 function
one to verify if block X and another to verify if block Y
so you need also to boolean variables to instead that one
canPassX
canPassY



Offline SonicPixelation

Senior Newbie





« Reply #2 - Posted 2013-09-04 16:17:19 »

I am not sure but I am guessing you ment something like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 public void CollisionManager(int delta) {
      int x0 = (int)(pos.x + (vel.x * delta));
      int y0 = (int)(pos.y + (vel.y * delta));
      int x1 = (int)(pos.x + width + (vel.x * delta));
      int y1 = (int)(pos.y + height + (vel.y * delta));
     
      List<Entity> entities = world.getEntities(x0, y0, x1, y1);
      for(int i = 0; i < entities.size(); i++){
         Entity e = entities.get(i);
         if(this == e) continue;
            this.touchedBy(e);
           
            if(this.blocksX(e)){
               canPassX = false;
            }
if(this.blocksY(e)){
               canPassY = false;
            }
         }
     
   }


and yeah I like that idea but it conflicts with my intersects method
1  
2  
3  
4  
5  
6  
7  
8  
9  
 // if the player intersects a object with 4 side
   public boolean intersects(int xt0, int yt0, int xt1, int yt1) {
      int xe0 = (int) (pos.x);
      int ye0 = (int) (pos.y);
      int xe1 = (int) (pos.x + width);
      int ye1 = (int) (pos.y + height);
      return !(xe1 < xt0 || ye1 < yt0 || xe0 > xt1 || ye0 >  yt1);

   }


the reason I didn't seprerate this in the first place is becaues I have done tile collision that slides in the past that didn't need to be seperated.
the tile collision I did in the past was simalar to the getEntities(int x0, int y0, int x1, int y1) method in the world class
and I sepeate my move method logic by calling it twice as move(xa,0) and move(0,ya)

so I think it should work but I am guessing there is a mistake somewhere that I didn't notice

what happens is when a entity hits another entity it sticks and I wan't it to be able to slide
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Several Kilo-Bytes

Senior Devvie


Medals: 11



« Reply #3 - Posted 2013-09-04 16:31:12 »

Collision is easier if you use prediction instead of moving everything first and rolling back collisions afterward. It also prevents the phenomenon called "tunneling". With this model you can use "real world" physics. So not only can you slide on surfaces, you can also use bouncing and friction. To slide, you project your velocity vector on to a line, and to bounce you reflect off a line. (Look up the math and diagrams for both.) If the walls are axis aligned, you just set the x or y velocity to zero. Then you use your simulation with your new velocity and position for the remaining duration of the turn. You must "stop" the player before it touches the wall so that you do not intersect with it at the end of the update or while still applying sliding.
Offline alesky

Junior Devvie


Medals: 3
Exp: 15 years


mmm....


« Reply #4 - Posted 2013-09-04 16:39:08 »

you can continue to work also with this logic
also the intersection function is ok that mean that this two rectangles intersect in same point
and you are shore 100%

the problem is after:
understand where, in what side, they intersect so that you can decide where you can move

the chance can be four

A over B
    ___
   |     |
   | A  |
   |___|
   |     |
   | B  |
   |___|

this case is when min Y A < max Y B
come move X = true
come move Y = false

A under B
    ___
   |     |
   | B  |
   |___|
   |     |
   | A  |
   |___|

this case is when max Y A > max Y B
come move X = true
come move Y = false

A before B
    _______
   |     |     |
   | A  |  B |
   |___|__ |
  
this case is when max X A > min X B
come move X = false
come move Y = true

A after B
    _______
   |     |     |
   | B  |  A |
   |___|__ |
  
this case is when min X A < max X B
come move X = false
come move Y = true


now
the 2 case
A over B  
A under B
are checked inside the new function blockY

the 2 case
A before B  
A after B
are checked inside the new function blockX


i hope is cleare
also my hash art Smiley

Offline SonicPixelation

Senior Newbie





« Reply #5 - Posted 2013-09-04 16:54:17 »

something I messed up and would like to correct

the move method was ment to be like this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void move(float xa, float ya, int delta){
    if(xa != 0 && ya != 0){
     move(xa,0, delta);
     move(0,ya,delta);
     return;
    }
   CollisionManager(delta);
      if(canPass){
         pos.x += xa * delta;
         pos.y += ya * delta;
      }
   }

this is what the code was ment to look like but it still errors and dosen't let me slide on walls
Offline SonicPixelation

Senior Newbie





« Reply #6 - Posted 2013-09-04 17:05:47 »

Thanks everyone I found the problem the collisonManager class was calling the global velocity for the method and wasent spliting them up
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.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (60 views)
2014-12-09 22:41:13

BurntPizza (95 views)
2014-12-08 04:46:31

JscottyBieshaar (55 views)
2014-12-05 12:39:02

SHC (71 views)
2014-12-03 16:27:13

CopyableCougar4 (73 views)
2014-11-29 21:32:03

toopeicgaming1999 (133 views)
2014-11-26 15:22:04

toopeicgaming1999 (124 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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