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

Matthew Tory
  • 1,306
  • 2
  • 16
  • 30

1 Answers1

2

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

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    That is a good idea, but I'm 14 and I have no idea what any of the means :/ – Matthew Tory Apr 20 '12 at 01: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. – Andrew Thompson Apr 20 '12 at 01:57
  • Is there any APIs that you know of that I should use? I already know of slick2D which looks good – Matthew Tory Apr 20 '12 at 01:58
  • You might also look at the more basic examples in this related [Q&A](http://stackoverflow.com/q/9849950/230513). At the other end of the spectrum, [*Filthy Rich Clients*](http://filthyrichclients.org/) shows how to make your own framework. – trashgod Apr 20 '12 at 02:05
  • would there be a way to do it without using a custom API? or without extremely complicated maths? just semi complicated? anything – Matthew Tory Apr 20 '12 at 02:34