• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
newbie_game_dev

Push rectangle to where the collision happens?

1 post in this topic

Hi. I have a problem in tile collision. I am using Libgdx framework. I got the working tile collision base on their example the problem though is that is collision happens I cant move my sprite. For example if i have a collision in right side I cant move my sprite to move up or down :( Here is my code

 int startX, startY, endX, endY;
        float pushx = 0,pushy = 0;
        // move player
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            dx=-1;
            currentWalk = leftWalk;

        }
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            dx=1;
            currentWalk = rightWalk;

        }
        if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
            dy=-1;
            currentWalk = downWalk;
        }
        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            dy=1;
            currentWalk = upWalk;
        }



        sr.setProjectionMatrix(camera.combined);
        sr.begin(ShapeRenderer.ShapeType.Line);
        Rectangle koalaRect = rectPool.obtain();
        koalaRect.set(player.getX(), player.getY(), pw, ph /2 );

        float oldX = player.getX(), oldY = player.getY(); // THIS LINE WAS ADDED

        player.setXY(player.getX() + dx * Gdx.graphics.getDeltaTime() * 4f, player.getY() + dy * Gdx.graphics.getDeltaTime() * 4f); // THIS LINE WAS MOVED HERE FROM DOWN BELOW



            if(dx> 0) {
                startX = endX = (int)(player.getX() + pw);
            } else {
                startX = endX = (int)(player.getX()  );
            }
            startY = (int)(player.getY());
            endY = (int)(player.getY() + ph);
            getTiles(startX, startY, endX, endY, tiles);

            for(Rectangle tile: tiles) {
                sr.rect(tile.x,tile.y,tile.getWidth(),tile.getHeight());
                if(koalaRect.overlaps(tile)) {

                        //dx = 0;
                        player.setX(oldX); // THIS LINE CHANGED
                        Gdx.app.log("x","hit  " + player.getX() + " " + oldX);
                        break;
                    
                }
            }






            if(dy > 0) {
                startY = endY = (int)(player.getY() + ph );
            } else {
                startY = endY = (int)(player.getY() );
            }
            startX = (int)(player.getX());
            endX = (int)(player.getX() + pw);
            getTiles(startX, startY, endX, endY, tiles);

            for(Rectangle tile: tiles) {
                if(koalaRect.overlaps(tile)) {
                    //dy = 0;
                    player.setY(oldY); // THIS LINE CHANGED
                    //Gdx.app.log("y","hit" + player.getY() + " " + oldY);
                    break;
                }
            }


        sr.rect(koalaRect.x,koalaRect.y,koalaRect.getWidth(),koalaRect.getHeight() / 2);
        sr.setColor(Color.GREEN);
        sr.end();

The solution i think is to push back the sprite when collision happens, but i dont know how. pls help :( thanks

 

0

Share this post


Link to post
Share on other sites

If I'm reading your code right then:

 

1. koalaRect is set to players original location

2. Player is then moved to new position: X + dx, Y + dy

3. If (koalaRect is overlapping something it shouldnt) move player back to old position

 

As you can see, you are testing collision using old player position before adding (dx, dy),

and that means that collisions only happen once the player is technically already stuck. smile.png

 

Move the line that sets the koalaRect to after setting new player position to immediately fix the problem,

or simply set koalaRect to player.X + dx, player.Y + dy, and only upon confirming the player won't be stuck there,

move the player.

 

Finally, it looks like you are trying to check player.X + dx and player.Y + dy independently, which is exactly what you want to do,

however, again koalaRect isn't updated, so the second collision check is the very same test you performed just lines before.

To run independent tests for dx and dy:

 

koalaRect.set(player.getX() + dx, player.getY(), pw, ph);

 

if (collisionTest(koalaRect) == false)

{

   // (player.X + dx, player.Y) did not collide with anything

   // move player on X-axis only:

   player.X += dx;

}

 

koalaRect.set(player.getX(), player.getY() + dy, pw, ph);

 

if (collisionTest(koalaRect) == false)

{

   // (player.X, player.Y + dy) did not collide with anything

   // move player on Y-axis only:

   player.Y += dy;

}

 

Hope this helps

Edited by Kaptein
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0