Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  Entity collision problem  (Read 126 times)
0 Members and 1 Guest are viewing this topic.
Offline kikoano

Junior Newbie





« Posted 2014-04-21 19:01:10 »

My level its made of entities(mobs , walls...) I want to make collision for them all in one especially for the walls all my entities have x and y also I have a move class here is my code.The Entity collision works but it has many problems(like if I hit the left wall it will continue to walk but it will go down and not pass the wall I want to not move at all and sometimes it goes up and down very fast when its stuck between walls)
Entity collision
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  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
private void entityCollision() {
      for (int j = 0; j < getMobs().size(); j++) {
         for (int i = 0; i < getTile().size(); i++) {
            if (getMobs().get(j).getX() <= getTile().get(i).getX() + 22 && getMobs().get(j).getX() >= getTile().get(i).getX() - 6 && getMobs().get(j).getY() <= getTile().get(i).getY() + 30
                  && getMobs().get(j).getY() >= getTile().get(i).getY() - 15) {
               if (getMobs().get(j).getX() <= getTile().get(i).getX()) {
                  ((Mob) getMobs().get(j)).move(0, 0);
               } else {
                  ((Mob) getMobs().get(j)).move(1, 0);
               }
               if (getMobs().get(j).getX() >= getTile().get(i).getX()) {
                  ((Mob) getMobs().get(j)).move(0, 0);
               } else {
                  ((Mob) getMobs().get(j)).move(-1, 0);
               }
               if (getMobs().get(j).getY() <= getTile().get(i).getY()) {
                  ((Mob) getMobs().get(j)).move(0, 0);
               } else {
                  ((Mob) getMobs().get(j)).move(0, 1);
               }
               if (getMobs().get(j).getY() >= getTile().get(i).getY()) {
                  ((Mob) getMobs().get(j)).move(0, 0);
               } else {
                  ((Mob) getMobs().get(j)).move(0, -1);
               }
            }
         }
      }
      for (int j = 0; j < getPlayers().size(); j++) {
         for (int i = 0; i < getTile().size(); i++) {
            if (getPlayers().get(j).getX() <= getTile().get(i).getX() + 22 && getPlayers().get(j).getX() >= getTile().get(i).getX() - 6
                  && getPlayers().get(j).getY() <= getTile().get(i).getY() + 30 && getPlayers().get(j).getY() >= getTile().get(i).getY() - 15) {
               if (getPlayers().get(j).getX() <= getTile().get(i).getX()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(1, 0);
               }
               if (getPlayers().get(j).getX() >= getTile().get(i).getX()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(-1, 0);
               }
               if (getPlayers().get(j).getY() <= getTile().get(i).getY()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(0, 1);
               }
               if (getPlayers().get(j).getY() >= getTile().get(i).getY()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(0, -1);
               }
            }
         }
         for (int i = 0; i < getMobs().size(); i++) {
            if (getPlayers().get(j).getX() <= getMobs().get(i).getX() + 16 && getPlayers().get(j).getX() >= getMobs().get(i).getX() - 16
                  && getPlayers().get(j).getY() <= getMobs().get(i).getY() + 16 && getPlayers().get(j).getY() >= getMobs().get(i).getY() - 16) {
               if (getPlayers().get(j).getX() <= getMobs().get(i).getX()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(1, 0);
               }
               if (getPlayers().get(j).getX() >= getMobs().get(i).getX()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(-1, 0);
               }
               if (getPlayers().get(j).getY() <= getMobs().get(i).getY()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(0, 1);
               }
               if (getPlayers().get(j).getY() >= getMobs().get(i).getY()) {
                  ((Mob) getPlayers().get(j)).move(0, 0);
               } else {
                  ((Mob) getPlayers().get(j)).move(0, -1);
               }
            }
         }
      }
      System.out.println(getClientPlayer().getX() + " " + getClientPlayer().getY());
   }

in this code there are 3 parts collision with player and tiles(walls), with player and mobs, mobs and mobs(don't work at all)
and here is the move class this doesn't have problems I use this to move player and mobs
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  
public void move(double xa, double ya) {
      if (xa != 0 && ya != 0) {
         move(xa, 0);
         move(0, ya);
         return;
      }

      if (xa > 0)
         dir = Direction.RIGHT;
      if (xa < 0)
         dir = Direction.LEFT;
      if (ya > 0)
         dir = Direction.DOWN;
      if (ya < 0)
         dir = Direction.UP;
for (int x = 0; x < Math.abs(xa); x++) {
         if (!collision(abs(xa), ya)) {
            this.x += abs(xa);
         }
      }
      for (int y = 0; y < Math.abs(ya); y++) {
         if (!collision(xa, abs(ya))) {
            this.y += abs(ya);
         }
      }

and this is collsion class I use for the tiles(the tiles are generated from a picture each pixel its some tile they don't have x or y they have only sprite)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private boolean collision(double xa, double ya) {
      boolean solid = false;
      for (int c = 0; c < 4; c++) {
         double xt = ((x + xa) - c % 2 * 15) / 16; // Sprite collision fix
         double yt = ((y + ya) - c / 2 * 15) / 16;
         int ix = (int) Math.ceil(xt);
         int iy = (int) Math.ceil(yt);
         if (c % 2 == 0)
            ix = (int) Math.floor(xt);
         if (c / 2 == 0)
            iy = (int) Math.floor(yt);
         if (level.getTile(ix, iy).solid())
            solid = true;
      }
      return solid;
   }
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.

theagentd (16 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!