Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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  
  [Libgdx] Collision check [Solved]  (Read 1725 times)
0 Members and 1 Guest are viewing this topic.
Offline printer

Junior Devvie


Medals: 1



« Posted 2017-12-22 00:35:45 »

Hello,

i trying to figure out the best way to make collision check on my code. I would like to add it into onWalk and using getNextPosition to check if the position is walkAble. Monsters & Players share the same logic.

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  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
public class Creature {

    protected enum Direction {
        NORTH, EAST, SOUTH, WEST
    }

    protected final String name;
    protected Direction direction;
    private Vector2 targetPosition;
    protected Vector2 position;
    protected float speed;

    private Texture texture = new Texture("badlogic.jpg");

    protected boolean isMoving = false;

    public Creature(String name, Vector2 position) {
        this.name = name;
        this.position = position;
        targetPosition = new Vector2();
        speed = 4f;
        direction = Direction.SOUTH;
    }

    public void onThink(float deltaTime) {
        if (isMoving) {
            onWalk(direction, deltaTime);
        }
    }

    protected void onWalk(Direction direction, float deltaTime) {
        if (targetPosition.isZero()) {
            targetPosition.set(getNextPosition(direction, position));
        }

        if (position.dst(targetPosition) <= 0.1f) {
            isMoving = false;
            position.set(targetPosition);
            targetPosition.setZero();
            return;
        }

        switch (direction) {
            case NORTH:
                position.y += speed * deltaTime;
                break;
            case EAST:
                position.x += speed * deltaTime;
                break;
            case SOUTH:
                position.y -= speed * deltaTime;
                break;
            case WEST:
                position.x -= speed * deltaTime;
                break;
            default:
                break;
        }
    }

    public Vector2 getNextPosition(Direction dir, Vector2 pos) {
        pos = new Vector2(pos.x, pos.y);
        switch (dir) {
            case NORTH:
                pos.y++;
                break;
            case EAST:
                pos.x++;
                break;
            case SOUTH:
                pos.y--;
                break;
            case WEST:
                pos.x--;
                break;
            default:
                break;
        }

        return pos;
    }

    public Vector2 getPosition() {
        return position;
    }

    public boolean isCreature() {
        return true;
    }

    public Texture getTexture() {
        return texture;
    }

    public boolean isPlayer() {
        return false;
    }

    public boolean isMonster() {
        return false;
    }
}
Offline bmanmcfly
« Reply #1 - Posted 2017-12-27 21:07:44 »

Seems like you are going for smoothed grid based movement, right?

If that's the case, you would just add in code before starting the movement. 

I'm guessing your onThink would have a condition before deciding on where to move a function of isMoveValid, in which case you would test if the square is full.

Are you considering that things can collide only on a tile basis? If so, that's really easy, it's just a matter of checking if the target cell contains things that would prevent it from moving, iswall, isenemy, etc... maybe simply isblocked. depends on what you need/
Offline printer

Junior Devvie


Medals: 1



« Reply #2 - Posted 2017-12-28 19:14:12 »

How can i execute a check before it? Since when they are overlaping its already to late. It May get stuck or something.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bmanmcfly
« Reply #3 - Posted 2017-12-28 20:43:55 »

I understand your question a bit better...

This doesnt really tell me how your map is formatted or anything, so, the specifics on how you would check might be different...
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  
    public void onThink(float deltaTime) {
        if (isMoving) {
            onWalk(direction, deltaTime);
        }
    }

    protected void onWalk(Direction direction, float deltaTime) {
        if (targetPosition.isZero()) {
            targetPosition.set(getNextPosition(direction, position));
        }

        if (position.dst(targetPosition) <= 0.1f) {
            isMoving = false;
            position.set(targetPosition);
            targetPosition.setZero();
            return;
        }

    public Vector2 getNextPosition(Direction dir, Vector2 pos) {
        this.pos.set(pos); //Don't create a new vector each time you want to move, make a class vector instead
        switch (dir) {
            case NORTH:
                pos.y++;
                break;
            case EAST:
                pos.x++;
                break;
            case SOUTH:
                pos.y--;
                break;
            case WEST:
                pos.x--;
                break;
            default:
                break;
        }
        // do your collision check here,
if (isNextPosition())
if the move is valid return the desired position
if it is blocked return the current position (usually, unless you want the effect of walking into the wall)

    }
}


the is next position would be something like if desired position contains wall, or is flagged as a wall, or whatever then return true otherwise return false.

If you are using a tiled map its like
Cell cell;
cell = tiledmap.getcell(x, y);
I forget exactly...

Another way is to draw rectangles over the walls, then you can make a list of the rectangles and see if the rectangles.contain(desiredposition)

Offline printer

Junior Devvie


Medals: 1



« Reply #4 - Posted 2017-12-28 21:13:47 »

The problem i mostly have is how badly i structured the classes i guess. Since i can only check the overlaps of Rectangles on my GameScreen class and i have no idea what to do from there :/

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
        for (int i = 0; i < blockArray.size; i++) {
            // Fetch the current block
            Creature creature = blockArray.get(i);

            // Clear out the entities to check
            entitiesToCheck.clear();

            // Retrieve the entities we might be able to compare against
            quadTree.retrieve(entitiesToCheck, creature);

            // Check the blocks
            for (Creature spectator : entitiesToCheck) {
                // SPECIAL CASE: No need to check against our self
                if (creature == spectator) {
                    continue;
                }

                if (creature.getRectangle().overlaps(spectator.getRectangle())) {
                    System.out.println("Collision has occured");
                }
            }
        }
Offline bmanmcfly
« Reply #5 - Posted 2017-12-28 21:24:59 »

ok, so, you have things checking for overlaps that have already occurred, so, all you would need is to do that check on the rectangle representing the desired position.

in other words, find out what the desired position is then run that test on THAT position.

So, the logic is:
- determine where the collision is going to happen
- if no collision then move
- if theres a collision dont move, maybe attack, not sure what you need

I dont think youll have to re-write a whole lot, doesnt look like anything wrong, but you may need to reorganize things.
Offline printer

Junior Devvie


Medals: 1



« Reply #6 - Posted 2017-12-28 21:30:51 »

Well that is the thing i tried. With check if there is overlaps on the nextPosition. Which i got the function. But things still does not work :/
Offline bmanmcfly
« Reply #7 - Posted 2017-12-28 21:48:58 »

Sorry, I'm at a loss for what to suggest next, without looking through the project.

Best bet, Setup a bunch of logging commands (Gdx.app.log(tag, string)Wink this way you can make sure that you are sending and receiving the correct data.

You might be doing something off without realizing it.

Offline printer

Junior Devvie


Medals: 1



« Reply #8 - Posted 2018-01-12 13:48:03 »

Cheers mate, i was able to port your suggestion and works perfectly now.
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

nelsongames (1677 views)
2018-04-24 18:15:36

nelsongames (2313 views)
2018-04-24 18:14:32
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

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!