1
votes

Im developing a java game im trying to do collision detection right now with my tile map but it is not working how I would like it to. I have made 4 rectangles on the character. One on the top, bottom, left and right side. The rectangles are all 2 times the velocity in width.

To check if the rectangle intersects with the rectangle on the tiles, I use this code

if(LeftSide.intersects(Map.colRect[i])){
            MovingLeft = false;
            x_pos+=vel;

        }

To define the rectangle I use this code

LeftSide = new Rectangle(x_pos,y_pos+(vel*2),(vel*2),spriteHeight-1-(vel*4));
    RightSide = new Rectangle(x_pos+spriteWidth-1,y_pos+(vel*2),(vel*2),spriteHeight-(vel*4)-1);
    UpSide = new Rectangle(x_pos+(vel*2),y_pos,spriteWidth-(vel*4)-1,(vel*2));
    DownSide = new Rectangle(x_pos+(vel*2),y_pos+spriteHeight-1,spriteWidth-(vel*4)-1,(vel*2)); 

What happens is when the player hits the wall, goes into the wall as much as the velocity, and then gets pushed back out of the wall the amount of the velocity. This results in the character to just go back and forth making a blurry motion whenever you hit a wall and hold down the key.

Is there an algorithm I could use to fix this? or a different method?

The rectangles on the character look like this: Rectangles

Any help would be greatly appreciated. I really want to fix this

Thanks

1
hmm .. when bounced out of the wall, why not just set speed to zero?JustJeff

1 Answers

2
votes

You probably want to model elastic collisions, discussed here and illustrated here.