1

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
    Apr 20, 2012 at 1:39

1 Answer 1

2

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

5
  • 1
    That is a good idea, but I'm 14 and I have no idea what any of the means :/ Apr 20, 2012 at 1:52
  • I suspect you should be looking at using a gaming API that will do much of this type of stuff for you. It will also help get around the (almost inevitable) problems with frame rates & graphics painting in general. Game development is a very specialized area, so it won't be easy. Apr 20, 2012 at 1:57
  • Is there any APIs that you know of that I should use? I already know of slick2D which looks good Apr 20, 2012 at 1:58
  • You might also look at the more basic examples in this related Q&A. At the other end of the spectrum, Filthy Rich Clients shows how to make your own framework.
    – trashgod
    Apr 20, 2012 at 2:05
  • would there be a way to do it without using a custom API? or without extremely complicated maths? just semi complicated? anything Apr 20, 2012 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.