Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  Colission Detection + ECS  (Read 480 times)
0 Members and 1 Guest are viewing this topic.
Offline theOne

Senior Newbie





« Posted 2013-10-05 20:53:14 »

Hey I wrote a while back a Colission Detection which works more or less.
The problem is that if i am pressing up and left or right and above me is a colission Tile my Sprite goes the whole way right/left to the last colission Tile and I dont get why.

And how can I make it more intuitive? Because I want to be able to rotate Sprite, so they are not just looking in one of the four directions.

atm, I am doing it so:
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  
TiledMapTileLayer colissionLayer = (TiledMapTileLayer) gameMap.getLayers().get("colission");

        Vector3 colissionTile = null;
        if (colissionLayer != null || colissionOn) {
            if (pseudoPlayer == null) this.pseudoPlayer = new Player(player.getPostition(), player.getImage());
            pseudoPlayer.setFacing(player.getFacing());
            pseudoPlayer.setX(player.getX());
            pseudoPlayer.setY(player.getY());
            pseudoPlayer.update(elpasedTime);
            colissionTile = Collision.getTileColission(pseudoPlayer, colissionLayer, "blocked");
        }
        if (colissionTile == null) {
            player.update(elpasedTime);
        } else {
            switch (player.getFacing()) {
                case RIGHT:
                    player.setX(colissionTile.x - player.getWidth() - 1);
                    break;
                case LEFT:
                    player.setX(colissionTile.x + getTileSize() + 1);
                    break;
                case UP:
                    player.setY(colissionTile.y - player.getHeight() - 1);
                    break;
                case DOWN:
                    player.setY(colissionTile.y + getTileSize() + 1);
                    break;
            }
        }

    public static Vector3 getTileColission(IEntiy entity, TiledMapTileLayer layer, String term) {
        float y = entity.getY(), x = entity.getX();
        float TILE_SIZE = layer.getTileWidth();

        int tileX = (int) (x / TILE_SIZE);
        int tileY = (int) (y / TILE_SIZE);

        Vector3 tileA = new Vector3(x, y, 0);
        Vector3 ereg = new Vector3(0, 0, 0);
        int width = (int) entity.getWidth();
        int height = (int) entity.getHeight();

        tileA.x = tileX * TILE_SIZE;
        tileA.y = tileY * TILE_SIZE;
        if (intersects(entity, tileA, 32, 32) && !tileisFree(tileX, tileY, layer, term)) {


            return ereg.set(World.tilesToPixels(tileX, (int) TILE_SIZE), World.tilesToPixels(tileY, (int) TILE_SIZE), 0);
        }

        x += width;
        tileX = (int) (x / TILE_SIZE);
        if (intersects(entity, tileA, 32, 32) && !tileisFree(tileX, tileY, layer, term)) {
            return ereg.set(World.tilesToPixels(tileX, (int) TILE_SIZE), World.tilesToPixels(tileY, (int) TILE_SIZE), 0);
        }

        x -= width;
        y += height;
        tileY = (int) (y / TILE_SIZE);
        tileX = (int) (x / TILE_SIZE);
        if (intersects(entity, tileA, 32, 32) && !tileisFree(tileX, tileY, layer, term)) {
            return ereg.set(World.tilesToPixels(tileX, (int) TILE_SIZE), World.tilesToPixels(tileY, (int) TILE_SIZE), 0);
        }

        x += width;
        tileX = (int) (x / TILE_SIZE);
        if (intersects(entity, tileA, 32, 32) && !tileisFree(tileX, tileY, layer, term)) {
            return ereg.set(World.tilesToPixels(tileX, (int) TILE_SIZE), World.tilesToPixels(tileY, (int) TILE_SIZE), 0);
        }

        return null;
    }

So, how can I avoid that?


I recently discovered Entity Systems and like the concept so far.
How should I make an Colission System? And how to communicate between the Systems?
I thought about just processing the Movement System and then a collide System. If in the collide System a colission is detected it would set the sprite to the next safe place.
How to make an InputComponent? I thought about making just an COmponent with a List<Input.keys, Gameaction> this is then rendered by an System which checks if and key of the COmponent is pressend and then takes the GameAction and performs and Action depending on the GameAction.


Edit PseudoCode:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
method1(){
if(checkifColissionLayer){
    generate pseudoPlayer; set values to values of Player
    update pseudoPlayer;
    Vector3 clollTile= checkColission(pseudoPlayer)   //collTile is bottom left pixel of Tile
}
if(colltile==null) player.update;  //no colission
else
adjust player position depending on Direction he moved last
so, if the player last pressed up and there is an colission -> above me is an colissionTile -> calculate nearest pixel to the colissionTile
}

Vector3 checkColission(Player p,Layer l, String term){ //l... The layer where colissionTiles are exppected;  term... the property of a Tile if is colidable
tileX,TileY = The Tile, the player is standing on

calculate for all four corners the pixel Position and then change it back to the x,y coordinate of the Tile
if the player intersects with this Tile and the Tile is not free -> return bottom left Vector of the Tile the colission happened.

}

I already read many articles about Colission Detection, but didnt undersand them. So developed this one for my own. Although I knew that its not very sophisticated.
Offline theOne

Senior Newbie





« Reply #1 - Posted 2013-10-07 18:41:32 »

Well, has nobody an clue whats wrong?
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2013-10-07 19:24:00 »

I have no clue how your game looks, so i cant really tell how you make something more intuitive.

About the collission however, try to check both x, and y axis, and calculate the smallest path to some free space.
I dont really get your code, so i dont know what you are doing atm.
Maybe you could just write down in steps how you are checking colission now.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theOne

Senior Newbie





« Reply #3 - Posted 2013-10-08 17:02:31 »

Okay, thx.
Updated the first post, hope somebody can now imagine what I am doing. But I basiclly guess that my Player has such a bad behaviour, due to the Input of more than one Key.
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.

trollwarrior1 (27 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
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!