Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 25, 2013 6:12 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri Mar 23, 2012 3:29 am 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
hello, I was facing a problem regarding the collision detection on my game here's the code on the update loop

Code:
Input input = gc.getInput();
        if (input.isKeyDown(Input.KEY_UP)) {
            Vector2f position = owner.getPosition();
            CollidableObject colObj = isCollidingWithEntities();
            if (colObj != null) {
                System.out.println(colObj.getPosition().getY());
                position.y =  colObj.getCollisionShape().getCenterY() + 16;
                System.out.println("position reset:"+position.y);
            }else{
                position.y -= MOVE_AMOUNT * i;
            }
            owner.setPosition(position);
        }

        if (input.isKeyDown(Input.KEY_DOWN)) {
            Vector2f position = owner.getPosition();
            CollidableObject colObj = isCollidingWithEntities();
            if (colObj != null) {
                System.out.println("colObj:"+colObj.getPosition());
                System.out.println("owner:"+owner.getPosition());
                System.out.println("collision will happen");
           
            }else{
                    System.out.println("no collision");
                position.y += MOVE_AMOUNT * i;
            }
            owner.setPosition(position);
        }


heres the code for the isCollidingWithEntities method
Code:
private CollidableObject isCollidingWithEntities() {
        playerEntity = new CollidableObject(playerShape, owner.getPosition());

        for (CollidableObject colObj : collidableObjects) {
            if (colObj.isCollidingWith(playerEntity)) {
                System.out.println("There will be a collision");
                return colObj;
            }
        }
        return null;
    }


heres the output when i move the player to collide with a wall when pressing the up key.

Quote:
There will be a collision
colObj:[Vector2f 992.0,1696.0 (1964.8104)]
owner:[Vector2f 1022.5,1727.5 (2007.4269)]


here's the link of the collision tile and the position of the player entity. the white box is the tile collided and the red is the player.
Image

the tile is 32x32 px also the player entity is 32x32 px also. when pressing the upkey with collision i was placing the player entity on the bottom of the collided tile so that it wouldn't pass through. but when pressing the down button it still detect a collision even if i placed it correctly.. did i miss something?


Last edited by wakkana12 on Sat Mar 24, 2012 6:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Collision problem
PostPosted: Fri Mar 23, 2012 4:18 am 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
ok. i think the problem was using the Polygon as its shape.. but when using Rectangle class as its shape its working fine now. but now my problem is that when continue pressing the up key the screen shakes. and the player entity sometimes passes through on the wall/colliding object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group