Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Show Posts Pages: [1]
 1 Game Development / Newbie & Debugging Questions / Re: Problems with rotation around 0-360 transition on: 2014-12-19 06:39:55 It is pretty messy, but it works, thank a lot.How would you suggest I do things to avoid such confusing series of if statements? Should I try to work in radians instead?
2  Game Development / Newbie & Debugging Questions / Problems with rotation around 0-360 transition on: 2014-12-19 05:46:24
Hello, I am having a simple problem I cant quite sort out. I have a sprite that is being rotated to point towards the mouse position. I have this all working so far with degrees, 0-360 for both the mouse and object. My problem is when I come to the transition, when the angle changes abruptly from 0 to 360 degrees.

Here is an image I made of the problem
http://i.imgur.com/BxFnVHy.png

I'm pretty sure that I need to add another condition to both if statements, probably an OR, but I cannot seem to figure out the correct condition.
 1  2  3  4  5  6 `        if (degreesToTheMouse > ship.angleInDegrees) {            ship.turnRight();        }        if (degreesToTheMouse < ship.angleInDegrees) {            ship.turnLeft();        }`

 1  2  3  4  5  6  7  8  9  10  11  12  13 `    public void turnRight() {        angleInDegrees += rotationSpeed * Gdx.graphics.getDeltaTime();        if (angleInDegrees >= 360) {            angleInDegrees = 0;        }    }    public void turnLeft() {        angleInDegrees -= rotationSpeed * Gdx.graphics.getDeltaTime();        if (angleInDegrees <= 0) {            angleInDegrees = 360;        }    }`

I have tried working with difference between the two angles, rather then working with the comparision of them, but I run into the same problem. (mouseAngle - shipAngle) and (shipAngle - mouseAngle) both have the same problem when the angle measurement flips from 0 to 360, or vice versa.

Can anyone offer any tips to help me? I'd greatly appreciate it.
3  Game Development / Newbie & Debugging Questions / Re: Moving towards the direction that is facing [Slick2D] (STILL NEED HELP) on: 2014-11-29 09:51:32
Change
 1 `collisionRect = new Rectangle(x, y, 120, 200);`

to
 1 `collisionRect = new Rectangle(x, y, 200, 120);`

Should probably fix your issue. That or set some kind of initial 90 degree rotation on the rectangle
4  Game Development / Newbie & Debugging Questions / Re: Simple rectangle collision problem (PacMan clone) on: 2014-11-14 08:43:11
I ended up solving my problem, and it was quite simple.

I was doing
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `// check for collisions in X or Y directions        if(detectCollision(new Rectangle(playerRect.x+velX, playerRect.y, playerRect.width, playerRect.height))){            collidingX=true;            velX = 0;        }        if(detectCollision(new Rectangle(playerRect.x, playerRect.y+velY, playerRect.width, playerRect.height))){            collidingY=true;            velY = 0;        }                if (!collidingX) {            position.x += velX / 16;            playerRect.x += velX;        }        if (!collidingY) {            position.y += velY / 16;            playerRect.y += velY;        }`

When I should be doing
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `// check for collisions in X or Y directions        if(detectCollision(new Rectangle(playerRect.x+velX, playerRect.y, playerRect.width, playerRect.height))){            collidingX=true;            velX = 0;        }        if (!collidingX) {            position.x += velX / 16;            playerRect.x += velX;        }        if(detectCollision(new Rectangle(playerRect.x, playerRect.y+velY, playerRect.width, playerRect.height))){            collidingY=true;            velY = 0;        }                       if (!collidingY) {            position.y += velY / 16;            playerRect.y += velY;        }`

I was doing AABB when I should have been doing ABAB.
5  Game Development / Newbie & Debugging Questions / Re: Simple rectangle collision problem (PacMan clone) on: 2014-11-13 00:16:52
I have figured out where my problem lies.
In the following code:

 1  2  3  4  5  6  7  8 `        if (!collidingX) {            position.x += velX / 16;            playerRect.x += velX;        }        if (!collidingY) {            position.y += velY / 16;            playerRect.y += velY;        }`

I failed to take into account the situation where (collidingY && collidingX), such as when the player is in a corner. I have tried putting the player back to their previous location, but that just causes a strange jiggle and no real progress.
 6 Game Development / Newbie & Debugging Questions / Simple rectangle collision problem (PacMan clone) on: 2014-11-12 21:20:40 I am struggling with some basic rectangle collision in my pacman clone, hoping for some help or simplification.So I am using libgdx Tiled maps, in which I have made an object layer, and surrounded all the collide-able things with rectangles. My player is also represented as a rectangle, and I use these to handle collision.Here is my problem graphically (I made debug render classes to help see the problem):Holding right button, then right + up, when approaching corner.Holding right button and up button, player now stuck. Must press left, then up, to move up.Essentially, I have sliding on the walls working, but when I get to a corner, the rectangles mash together and block movement. I want this to slide around the corner, and not get stuck.I know this is probably something really simple I am overlooking, but I cant quite place my finger on it.Here is my rough Player movement code:http://pastebin.java-gaming.org/c3cf63e321b1c1. First I add a number to velX or velY based on input.2. Then I check the future player rectangle, using the velocities from 1. to determine if the player will collide in the X or Y directions.3. Finally, if the player is not colliding in X, move player in X.4. If the player is not colliding in Y, move player in Y.It seems like I need to have code that says "As soon as the player can move up, move up and stop all X movement", but I cannot figure out how to implement it.I would greatly appreciate some help with my problem. Thanks.
 7 Game Development / Newbie & Debugging Questions / Re: [libGDX] Questions about scrolling 2D Textures - Slot machine on: 2014-03-23 06:46:06 This is my second post without any response. Am I asking a strange or wrong question?
 8 Games Center / WIP games, tools & toy projects / Re: Harvest Online on: 2014-03-23 01:47:31 Looks great! I'm working on a similar game myself using LibGDX, maybe we can collaborate! I'm only aiming for single player, but currently I'm running into problems with my design that I will have to rework.https://dl.dropboxusercontent.com/u/24222531/game.pngI wont  insert the image as to not confuse people between our games ^^
9  Game Development / Newbie & Debugging Questions / [libGDX] Questions about scrolling 2D Textures - Slot machine on: 2014-03-21 21:28:00
Hello, I'm trying to make a slot machine just as a fun project. Here is a link to it if you're interested.
https://dl.dropboxusercontent.com/u/24222531/SLOTS.zip
I made this simple one in about an hour, but I want to improve it.

In my first attempt, each slot machine "reel" consists of 3 sprite images placed in a column. The images then flip to the next image, making a cheap sort of scrolling.

I'm interested in using an actual scrolling sprite to implement my slot machine reels, but I am confused how to do it.
I found this tutorial https://code.google.com/p/libgdx-users/wiki/ScrollingTexture which helped me get the basics of scrolling a texture, but now I am struggling with indexing where the actual images are. Here is some code:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 `public ScrollWheel(){        Texture tex = new Texture(Gdx.files.internal("assets/wheel2.png"));        tex.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);        sprite = new Sprite(tex);        sprite.setSize(64, 256);        sprite.setPosition(150, 120);            }    public void update() {        scrollTimer += Gdx.graphics.getDeltaTime();        if (scrollTimer > 1.0f) {            scrollTimer = 0.0f;        }        sprite.setV(-scrollTimer);        sprite.setV2(-scrollTimer + 3f/6f);                }`

This successfully makes the image as large as I want, and also makes it scroll downwards like a slot machine reel would. Since my source image consists of 6 seperate images, each individual picture on the reel is some N/6 (thus the 3f/6f makes my reel display 3 images out of 6 at once).

My issue is identifying where the actual images are on the scrolling texture. When the player stops the reel, it needs to stop at the nearest actual image at the midpoint of the reel. This is quite tricky. Does anyone have any suggestions of how I would accomplish this? The V and V2 coordinates are a bit confusing.

Another method I considered was to compose a larger "reel" texture from the 6 individual textureRegions, and maybe use those as indexes somehow?
10  Game Development / Newbie & Debugging Questions / Designing an "interaction" system for tile game on: 2014-03-14 22:55:40
I'm a bit stumped with my project. I am using Tiled and libGDX, and I need to come up with a good way of interacting with different tiles. Currently, my player can only interact with one kind of tile but I need many more. Here is my working code at the moment:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `  this.cropLayer = PlayState.getLevelManager().getCurrentLevel().getCropLayer();  private void handleCropAction() {        if (cropLayer.getCell(facingTileX, facingTileY)                .getTile()                .getProperties()                .containsKey("crop")) {            if (liftItem(facingTileX, facingTileY)) {                return;            }            PlayState.getLevel().getCrops().getCropTile(facingTileX, facingTileY).advanceState();            System.out.println("advanceTile (" + facingTileX + ", " + facingTileY + ")");            ACTION = false;        }    }`

So I ask the level for a tile layer, then check if the cell I'm looking at contains the property "crop".

My issue is that I need to do with with multiple types of tiles: signs, beds, water, doors, and chest. I know my current method will not scale up as well and will become a giant mess, and I need some help designing a more general kind of interaction system.

I was considering creating an interface Interaction as follows
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `package Levels.Interactions;import Levels.Interactions.InteractionLayer.INTERACTION;public abstract class Interaction {     public INTERACTION type;        public Interaction(INTERACTION typeIn){        this.type = typeIn;                   }            public abstract void act();    public abstract void update();    public abstract void render();}`

and creating one 2D array of these Interactions from all map layers combined (Code here: http://pastebin.java-gaming.org/e0c1e796e84). From there I would make more classes implementing that state IE: Sign Interaction extends Interaction. Does this system seem adequate? Has anyone else done something similar before and could share some tips with me?

I also am struggling a bit with states and how to interrupt gameplay for things like text boxes. I have GameStates:Play, Menu, etc, but for UI interrupting gameplay I feel that my Play state needs sub-states such as "playing", "dialog", etc. Any tips regarding this would be appreciated as well.
 11 Game Development / Artificial Intelligence / Re: AI Challenge: '2048' on: 2014-03-12 09:03:39 I'm finding it very hard to even understand how to solve the puzzle, let alone program something to do it for me. There seems to be a somewhat random element in that after every move, a random block is chosen (of all blocks, including those with 2's already and excluding those with 4 and above), then a 2 is placed there.Sometimes this will make it look like a 4 was spawned.Currently, my bot would spam WASDWASDWASDWASD. I have gotten over 5000 points with that method, though sometimes its as low as 2000.
 12 Game Development / Newbie & Debugging Questions / Re: [LIBGDX] scene2d minimap on: 2014-03-12 06:16:19 https://github.com/libgdx/libgdx/wiki/Scene2d.ui Is a good place to start.I just started looking into this to make a HUD, though it is tricky and I havent gotten far.
13  Game Development / Newbie & Debugging Questions / Re: How do you fix a game loop flaw? on: 2014-03-10 19:12:00
Your game loop doesnt include any sort of Limiting variable, so yes, you will get faster frame speeds at certain times. Here is an example game loop I used before which includes a limit.
 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 `public void run() {        init();                double previously = System.nanoTime();        double limit = 1000000000 / 20D;         double timer = 0;        while (running) {            double now = System.nanoTime();            //Get the elapsed time since last cycle            double whileElapsed = (now - previously);            previously = now;            timer += whileElapsed; // Add the elapsed time to our timer            //Has it been more than our time limit?            if (timer >= limit) {                gsm.getCurrentGameState().Tick();                                //To make sure we never get periods of very fast                while (timer >= limit) {                    //Ticking, should the app run slower than our limit                    timer -= limit;                }            }            render();        }        stop();    }`
 14 Game Development / Newbie & Debugging Questions / Re: [libGDX] Drawing sprites on top of TiledMap on: 2014-03-05 23:06:05 I solved my original problem, it was a silly mistake where I hadn't told the for loop how many times to loop properly, in order to draw all the tiles.I also solved a coordinate grid problem between TMX maps and double for loop arrays.Now I'm running into a tearing problem. Ive read all over threads here and other places about ways to fix this, but none seem to work.Note: this only happens every once in a while, not consistent, but will stick like this when I stand in certain places.I tried looking into using MathUtils.round on my player X,Y before using them to update the camera, but my camera is scaled. So if I pass it Integers, the camera is really jumpy.
15  Game Development / Newbie & Debugging Questions / Re: [libGDX] Drawing sprites on top of TiledMap on: 2014-03-05 21:20:12
Another thing I noticed might be wrong:

I'm using the format
 1 `SpriteBatch.draw(Sprite, xPos, yPos, width, height);`

but this one also exists:
 1 `Sprite.draw(SpriteBatch);`

Which one is correct?
16  Game Development / Newbie & Debugging Questions / [libGDX] Drawing sprites on top of TiledMap on: 2014-03-05 21:10:37
Hello! Im new to libgdx, and having some problems with drawing sprites over a tiled map. I got it to work successfully with the player sprite, but other sprites locked on map grid I cannot get to draw.

First: the explanation.
I am drawing a whole map of tiles, but some of the tiles need to be "crop" tiles, where the player can plant seeds, farm, etc and have the plant image update for differing conditions (watered/not watered/growth level).

What I am doing at the moment is setting a specific tile property to "crop" in Tiled, then creating a new CropTile in the same position.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `...cropLayer = (TiledMapTileLayer) map.getLayers().get("Tile Layer 1");...private void dealWithCrops() {        for (int row = 0; row < cropLayer.getHeight(); row++) {            for (int col = 0; col < cropLayer.getWidth(); col++) {                if (cropLayer.getCell(row, col)                        .getTile()                        .getProperties()                        .containsKey("crop")) {                    cropList.newCrop(row, col, LEVELID);                }            }        }    }`

My Crop constructor looks like this
 1  2  3  4  5  6  7  8  9  10  11 `public CropTile(int x, int y, int mapID) {        this.x = x;        this.y = y;        int mapID1 = mapID;        cropSprite = new Sprite();        backGroundSprite = new Sprite();        cropSprite.setSize(1, 1);        cropSprite.setPosition(x, y);        backGroundSprite.setSize(1, 1);        backGroundSprite.setPosition(x, y);`

Next comes a bunch of logic to determine which texture the sprite gets with sprite.setTexture(Texture). ( I am sure this bit is properly giving the sprites textures)
Then I later try to draw them with
 1  2  3  4  5  6  7  8 `public void render(SpriteBatch sb, OrthographicCamera cam) {        setSpriteImages();        sb.setProjectionMatrix(cam.combined);        sb.begin();          sb.draw(backGroundSprite, x, y, 1, 1);        sb.draw(cropSprite, x, y, 1, 1);        sb.end();    }`

I think my problem might have to do with mixing coordinate planes, IE: libGDX uses bottom left (0,0) while my code is implying (0,0) top left? I understand more code is probably needed, please let me know what to share. I got this method to work well with the player Sprite, I just cant seem to figure out many tiles.

What is the best way to have drawn tiles on top of TiledMapRenderer? Should I be editing the tiles within the TMX file, or is my method acceptable? Should I be passing one SpriteBatch and Camera around everywhere?
 17 Game Development / Newbie & Debugging Questions / Re: Libraries vs. Pure Java on: 2014-03-03 21:14:22 I started on my first game with just pure java. Then I needed audio, got a simple library for that. Next I needed TMX maps, got another little library for that.Then I realized libGDX has everything I needed. I spend about 2 months struggling to implement my game in pure java, then I just redid it all in about a week with libGDX. Way easier!
 18 Games Center / Showcase / Re: Asteroids on: 2014-03-02 05:56:33 I have to say, your UI looks really nice!I am following the same tutorial you used. Does he cover how to do such nice fonts and UI in later parts, or how did you accomplish that?
19  Game Development / Newbie & Debugging Questions / Re: Can I Improve this level loader? on: 2014-02-25 17:13:51
I have a similar system, but I use a program called Tiled to draw my maps.
Natively, Tiled outputs a few specific TMX map formats which you can get a java library to read (complicated), but I have had little success with.

I simply load my spritesheet into tiled, and draw a map. Then I export the .TMX map file as CSV. Once that is done, I simple open the .TMX with notepad, copy the relevant array of integers the program has made for me, and plunk it into my own text file. All I use tiled for is numbering the spritesheet really, though it has all kinds of advanced features.

This makes it really easy to design maps visually instead of with text.

 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 `package Manager.Map;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class MapFileLoader {    // size of map    private int numCols, numRows, spriteSheetIndex, layerID, MAPID;    // holds integer array of read map data    private int[][] parsedLayer;        private BufferedReader br;    private final String delims = "\\s+";    public MapFileLoader(String path) {        InputStream in = getClass().getResourceAsStream(path);        br = new BufferedReader(new InputStreamReader(in));        parseFileHeader(br);        parseMapFile(br);        close(br);            }    // PRIVATE METHODS    private void parseFileHeader(BufferedReader br) {        try {            MAPID = Integer.parseInt(br.readLine());            numCols = Integer.parseInt(br.readLine());            numRows = Integer.parseInt(br.readLine());            spriteSheetIndex = Integer.parseInt(br.readLine());            layerID = Integer.parseInt(br.readLine());        } catch (IOException | NumberFormatException e) {            e.printStackTrace();        }    }    private void parseMapFile(BufferedReader br) {        try {            parsedLayer = new int[numRows][numCols];            for (int row = 0; row < numRows; row++) {                String line = br.readLine();                String[] tokens = line.split(delims);                for (int col = 0; col < numCols; col++) {                    parsedLayer[row][col] = (Integer.parseInt(tokens[col]));                                                        }                            }        } catch (IOException | NumberFormatException e) {            e.printStackTrace();        }    }    private void close(BufferedReader br) {        try {            br.close();        } catch (IOException | NumberFormatException e) {            e.printStackTrace();        }    }        // PUBLIC METHODS    public int getWidth() {        return numCols;    }    public int getHeight() {        return numRows;    }        public int getSpriteSheetIndex(){        return spriteSheetIndex;    }    public int[][] getMapData() {        return parsedLayer;    }    public int getLayerID(){        return layerID;    }    public int getMapID(){        return MAPID;    }}`

 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 `package Manager.Map;import gfx.SpriteSheet;import java.awt.image.BufferedImage;public class MapTranslator {        // change int[][] into BufferedImage[][] using Spritesheets[]    private final int[][] parsedLayer;    private final BufferedImage[][] Map;    private final SpriteSheet ss1;        public MapTranslator(int[][] inputArray, SpriteSheet inputSS){        this.parsedLayer = inputArray;        this.ss1 = inputSS;        this.Map = new BufferedImage[parsedLayer.length][parsedLayer[0].length];                Translate();            }        private void Translate(){ // int array into image array        for (int y = 0; y < parsedLayer[0].length; y++){            for (int x = 0; x < parsedLayer.length; x++){                Map[x][y] = ss1.getSingleTile(parsedLayer[y][x]);            }        }    }    public BufferedImage[][] getMap(){        return Map;    }}`
20  Java Game APIs & Engines / Java 2D / Re: Issues with drawing layers of Graphics G and Graphics G2D on: 2014-02-25 00:41:16
Ok, so I found that my graphics g has a "create()" method, which returns a copy of the current graphics context.

 1  2  3  4 `public void render(Graphics g) {                Graphics preG2D = g.create();        Graphics2D g2d = (Graphics2D) preG2D;`

This has fixed my original problem, at least I think so. What do you think?
 21 Java Game APIs & Engines / Java 2D / Re: Issues with drawing layers of Graphics G and Graphics G2D on: 2014-02-25 00:38:07 I'm trying what you suggested, but getting an error.g.createGraphics(); :: cannot find symbol :: method createGraphics :: location variable g of type GraphicsWhat you're saying is that when I do "(graphics2D) g" cast, it actually changes the original g?
22  Java Game APIs & Engines / Java 2D / Issues with drawing layers of Graphics G and Graphics G2D on: 2014-02-24 23:10:25
Hello!
I'm working on my first real game. It is tile-based, and I am only using a library to play mp3's at the moment.

I added a lighting effect to my game with G2D RadialGradientPaint and setComposite, and I am having troubles drawing anything on top of that layer in regular graphics g.

This class draws a light circle around the player which gets darker towards the edges and allows me to adjust the radius, simulating night/day vision distance somewhat.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public void render(Graphics g) {          x = TileGame.getPlayer().getX() + 16;        y = TileGame.getPlayer().getY() + 16;                Graphics2D g2d = (Graphics2D) g;        java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float((float)x, (float)y);                float[] dist = {0.0f, 1.0f};        Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};        RadialGradientPaint p = new RadialGradientPaint(center, currentRadius, dist, colors);        g2d.setPaint(p);        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .6f));                g2d.fillRect(64, 64, 640, 640);               g2d.dispose();    }`

In my main game loop, I have multiple things being rendered in a specific order.

 1  2  3  4  5  6 `                .  .  .  .                levelManager.render(g);                               ui.render(g);                crops.render(g);                player.render(g);                                light.render(g);`

My issue is this: In the above snippet, I want to draw UI.render() after the light.render() effect (which uses g2d). For some reason, if I put them in this order:

 1  2  3  4  5  6 `                .  .  .  .                levelManager.render(g);                  crops.render(g);                player.render(g);                                light.render(g);                ui.render(g);`

None of the UI gets rendered, even ones which weren't within the original  g2d.fillRect(64, 64, 640, 640) area. It seems like I need to cast my g2d back into g? Not sure how to solve this, I would appreciate some assistance.
 23 Game Development / Newbie & Debugging Questions / Re: Debugging movement/collision code in 2d tile game on: 2014-02-22 08: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.
24  Game Development / Newbie & Debugging Questions / Re: Debugging movement/collision code in 2d tile game on: 2014-02-21 20: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();    }    `
 25 Game Development / Newbie & Debugging Questions / Re: Debugging movement/collision code in 2d tile game on: 2014-02-21 20:22:04 Quote from: trollwarrior1 on 2014-02-21 20:20:48Here you go, a full proof tutorial on collision within tiles.http://www.java-gaming.org/topics/2d-collision-detection/31488/view.htmlThank you, I will review this.
26  Game Development / Newbie & Debugging Questions / Re: Debugging movement/collision code in 2d tile game on: 2014-02-21 20: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.
27  Game Development / Newbie & Debugging Questions / Debugging movement/collision code in 2d tile game on: 2014-02-21 20: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?
 Pages: [1]
 EgonOlsen (73 views) 2018-06-10 19:43:48 EgonOlsen (54 views) 2018-06-10 19:43:44 EgonOlsen (73 views) 2018-06-10 19:43:20 DesertCoockie (253 views) 2018-05-13 18:23:11 nelsongames (154 views) 2018-04-24 18:15:36 nelsongames (154 views) 2018-04-24 18:14:32 ivj94 (895 views) 2018-03-24 14:47:39 ivj94 (156 views) 2018-03-24 14:46:31 ivj94 (808 views) 2018-03-24 14:43:53 Solater (172 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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