Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Debugging movement/collision code in 2d tile game  (Read 483 times)
0 Members and 1 Guest are viewing this topic.
Offline digdugdiggy

Senior Newbie





« Posted 2014-02-21 21:11:54 »

Hello, I'm working on a top down 2d game and am having some problems with collision detection. Here is a basic screenshot.



My issue involves buggy movement. If I spam movement keys near a wall, I can pass through and get stuck. I think my problem may be related to multiple threads, but I am not sure.

I have a text file grid of integers read in for collision, 1 = not passable, 0 = passable. My checkCollision function checks the proposed new player position for a collision, and disallows that movement if there is a tile.

I use a overridden Keymanager classhttp://pastebin.java-gaming.org/b9854254086 to alter Player's public variables UP, DOWN, LEFT, RIGHT to transfer keyboard to game logic.

Here is the player tick 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  
public class Player {
**SNIP SNIP**
public void tick() {
        if (moving) {
            updatePlayerPosition();
        }

        if (sprinting) {
            moveSpeed = SPRINT; // 16
       } else {
            moveSpeed = WALK; // 8
       }

        if (!moving) {
            if (up) {
                moveUp();
            }
            if (down) {
                moveDown();
            }
            if (right) {
                moveRight();
            }
            if (left) {
                moveLeft();
            }

        }

Here I update the player movement if they are still in the process of moving from one tile to another. http://pastebin.java-gaming.org/9854530468e
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private void updatePlayerPosition() {
       
        switch (facing) {
            case "north":
                if (!detectCollision(tileX, tileY - 1)) {
                    northAnimator.update(System.currentTimeMillis());
                    y -= moveSpeed;
                    if (y % 32 == 0) {
                        moving = false;
                        tileY = (int) pixelsToTileY(y);
                    }
                }
                break;
            case "south":
*SNIPSNIP*
        }
    }


This simply changes facing direction, sets moving to true, and lets the above method handle the rest. http://pastebin.java-gaming.org/4506e684f89
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
private void moveUp() { // move player up
       // if not facing north, make facing north

        if (!facing.equals("north")) {
            facing = "north";
        } else {

            // else, detect collision in the direction            
           if (!detectCollision(tileX, tileY - 1)) {
                moving = true;
            }
        }
    }


Somewhere in here there is a bug going on. I know the detectcollision method is working properly as I debugged it with println's. It seems that sometimes, while I am actively "Moving", my direction can change and place me in a wall. Note that this code has double collision checking, I tried with one and it simply let me into impassable walls, with two checks i actually get stuck in the wall and cant move at all.

Can anyone help me out here. Is there a simpler way of getting animated tile movement?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2014-02-21 21:14:35 »

Quote
I think my problem may be related to multiple threads, but I am not sure.

You shouldn't need multithreading for a simple 2D game.

Multithreading is a nightmare and introduces lots of problems. It's probably best you don't use it unless you're absolutely certain you need it.

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2014-02-21 21:16:51 »

You are most likely dealing with rounding issues with the player's position when checking against the tile coordinates on the map.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline digdugdiggy

Senior Newbie





« Reply #3 - Posted 2014-02-21 21:19:01 »

I'm doing stuff like this to start up my game, not sure if this is multithreading or just one thread getting started up?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class TileGame extends Canvas implements Runnable {

public synchronized void start() {
        if (running) {
            return;
        }
        running = true;
        gameThread = new Thread(this);
        gameThread.start();}
@Override
    public void run() {
*snipsnip*

    }


On second thought, I dont think it is multithreaded. I kinda followed a tutorial for the basic game loop stuff, and I think all this just starts up one thread for the game.
Offline trollwarrior1
« Reply #4 - Posted 2014-02-21 21:20:48 »

Here you go, a full proof tutorial on collision within tiles.

http://www.java-gaming.org/topics/2d-collision-detection/31488/view.html
Offline digdugdiggy

Senior Newbie





« Reply #5 - Posted 2014-02-21 21:22:04 »

Here you go, a full proof tutorial on collision within tiles.

http://www.java-gaming.org/topics/2d-collision-detection/31488/view.html

Thank you, I will review this.
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-02-21 21:33:58 »

Is that theChernoProject? If it is, I have to say he's an ok programmer, but one thing that has always bugged me is his use of threads. You don't need to manually create a thread just to start a game. There is absolutely (as far as I know) no advantage to it, and it really isn't a good design.

Offline trollwarrior1
« Reply #7 - Posted 2014-02-21 21:41:21 »

You need to create a separate thread for your game when you're using JFrame. If you don't, you will have problems with JFrame.
Offline digdugdiggy

Senior Newbie





« Reply #8 - Posted 2014-02-21 21:47:02 »

You need to create a separate thread for your game when you're using JFrame. If you don't, you will have problems with JFrame.

The great tutorial maker arrives! I was just reading your tutorial linked above! Thanks for it, I'm just fiddling with the LWJGL keyboard stuff there.

Also, how would I go about making a seperate thread for my jframe? I have no experience with threads really.
Here is my main, which pretty much just makes a frame and launches it. I assume I would have to modify this?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public static void main(String[] args) {
       TileGame game = new TileGame();
        game.setPreferredSize(new Dimension(TileGame.WIDTH * TileGame.SCALE, TileGame.HEIGHT * TileGame.SCALE));
        game.setMaximumSize(new Dimension(TileGame.WIDTH * TileGame.SCALE, TileGame.HEIGHT * TileGame.SCALE));
        game.setMinimumSize(new Dimension(TileGame.WIDTH * TileGame.SCALE, TileGame.HEIGHT * TileGame.SCALE));

        JFrame frame = new JFrame("Autumn Cornucopia");
        frame.setSize(TileGame.WIDTH * TileGame.SCALE, TileGame.HEIGHT * TileGame.SCALE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(game);
        frame.setLocationRelativeTo(null);
        frame.requestFocus();
        frame.toFront();

        frame.setVisible(true);

        //gsm = new GameStateManager();
       //gsm.start();
       game.start();
    }
   
Offline digdugdiggy

Senior Newbie





« Reply #9 - Posted 2014-02-22 09:22:35 »

Ok, regarding the initial problem:

Thanks to the tutorial, I figured it out. In the end I ran into a simple semantics problem.
The code above references a "CanMove" method which checks a tile to see if the player can move there, while my collision layer was using the term "DetectCollision".

It took me a bit to figure out that (CanMove = true) != (DetectCollision = true). One says "yes you can move there", the other says "yes there is a collision".

Now I am trying to alter this new movement method so I can have similar movement as before. IE: Press button,ignore input,  move player image a few pixels, repeat for X ticks until centered on a tile, allow input again.

Also! I noticed a slight bug in the tutorial code, which was mentioned. Even if the player wasnt moving, it continued to call the CanMove functions a total of 10+ times every tick.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (36 views)
2014-07-24 01:59:36

Riven (37 views)
2014-07-23 21:16:32

Riven (25 views)
2014-07-23 21:07:15

Riven (27 views)
2014-07-23 20:56:16

ctomni231 (57 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!