0
\$\begingroup\$

I have been trying to figure out why my collision tiles act so strange; I put I collision tile above a normal chunk based tile but it seems to not work - the collision tiles seem to appear in the wrong places.

Here is my code for tile collision:

protected boolean collisionWithTile(int Px, int Py){
    for(CollisionTile e: handler.getWorld().getCollisionMap().getCollisionTiles()){
        if(getCollisionBounds(0,0).intersects(e.getBounds())){
            return e.isSolid();
        }
    }
    return false;
}

Here's the code for getting collision bounds for entities (player):

public Rectangle getCollisionBounds(float xOffset, float yOffset){
    return new Rectangle((int) (position.x+ bounds.x+xOffset), (int) (position.y+bounds.y+yOffset), bounds.width,bounds.height);
}

collision bounds for tile and render:

 public void render(Graphics g){
    if(solid){
        g.setColor(Color.RED);
    }else{
       g.setColor(Color.CYAN);
   }
    g.drawRect( (int) ((tilePosition.x * Tile.TILE_WIDTH - handler.getGameCamera().getxOffset())+regionPosition.x*Tile.TILE_WIDTH), (int) (tilePosition.y * Tile.TILE_HEIGHT - handler.getGameCamera().getyOffset()+regionPosition.y*Tile.TILE_HEIGHT),Tile.TILE_WIDTH,Tile.TILE_HEIGHT);
    g.setColor(Color.black);
}

public Rectangle getBounds(){
    return new Rectangle( (int) ((tilePosition.x * Tile.TILE_WIDTH - handler.getGameCamera().getxOffset())+regionPosition.x*Tile.TILE_WIDTH), (int) (tilePosition.y * Tile.TILE_HEIGHT - handler.getGameCamera().getyOffset()+regionPosition.y*Tile.TILE_HEIGHT),Tile.TILE_WIDTH,Tile.TILE_HEIGHT);
}

The render works just fine but the collision is totally different.

\$\endgroup\$
  • \$\begingroup\$ In what specific way is your collision "totally different"? Can you show us a representative screenshot? \$\endgroup\$ – DMGregory Aug 9 '19 at 11:43
  • \$\begingroup\$ well, the position seems to be in a totally different place. but the rendering thing uses the same variables so I don't know what causes this behaviour \$\endgroup\$ – gigano Aug 9 '19 at 14:54
  • \$\begingroup\$ Just repeating "totally different" doesn't add new information. Is it always above? Does it sometimes appear to the left and sometimes to the right? Is it always the same distance away? Does the distance change based on the original position of the chunk? Is the spacing between chunks different in the collision version than the rendered version? etc. The specific way that it's different can give us a lot of clues about what the sources of the problem could be. \$\endgroup\$ – DMGregory Aug 9 '19 at 15:09
  • \$\begingroup\$ it seems to always be in a different place(aka left right up down) or not exist at all. because the render function displays them in the right place I actually don't know. the variable or math it uses to display or place the thing is the same so I would think that the collision is on the same position but it is not. the render of the collision (a rectangle around the tile) is the same as the rendering of the tile itself(apart from having some variables have different names). and because the rendering and the actual collision is not the same I have no way of knowing if it is position dependant \$\endgroup\$ – gigano Aug 9 '19 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.