- Aug 18, 2005
- 91
- 0
- 0
Hello everybody! I'm not new to the forums, I look at them often, however I post very little. I have a question on simple collision detection between two rectangles. I currently do not care about how one can test for a collision, I simply care about the redrawing aspect of two objects projected to collide.
So I have two rectangles, on a collision course. Their height, width , and y positions are exactly the same, the only thing that changes is their x position. I define the change in position for the left rectangle as 3 pixels (arbitrary number) added to x every frame of the animation. The left rectangle moves along to the right while the right rectangle is stationary (for the purpose of the logic, the right rectangle doesn't need to be moving). I know how basic collision detections works. Say we have a point defined by x + 50, which pertains to the top left corner of the left rectangle plus 50 for the width of that same rectangle. So i can write something such as if(x + 50 == top left corner of stationary rectangle). This is all fine and dandy BUT what happens if the top left corner of my rectangle is 51 pixels away from the top left corner of my right rectangle? so the change in x for the collision to occur would have to be 1 pixel for the test to check true. However my change in x is defined as 3 pixels... so on the next frame my top left corner of the left rectangle is 48 pixels away from the top right corner of the right rectangle. The collision test failed and now since my rectangle is 50 pixels wide, it is now penetrating the right rectangle and just decides to keep on moving every frame. I know simple solutions can be made such as if(x + 50 >= top left corner of right rect), blah blah. These are solutions I do not want. I want pixel perfect collision AND I also want the drawing of the rectangle to be fluent through the animation.
What I have tried so far that has seen the most success is adding my velocity vector (3) to x + 50 and then seeing if that pokes through the top left corner of the right rectangle (if(x + 50 + dx > top left corner of right rect)), THEN iterating through all the possible values of dx for(i = 0; i < dx; i++) --> if(x + 50 + i == top left corner right rect) then the change in x for that frame is equal to i. This makes it so that the left box arrives at the exact position of the right rectangle for the next frame and regular detection prevails. However the problem with this is that it changes the speed of my velocity vector for that frame, which is an incorrect solution.
I know I am missing something and have been doing a lot of reading online about collision detection and animation, but those articles seem to fail to provide me with the information I seek. I do not care to know how collisions are detected, but rather how animations are redrawn so pixel perfect detection can be made. I will let the actual detecting be a surprise when I actually care to learn those advanced methods.
PS. my frame rate is 60 fps. I have experimented with changing frame rates in accordance to the velocity vectors. However this is also to no avail. Eventually, this all will be integrated into a simple gravity, collision detection physics engine over the long haul. If such solutions involve calculus or physics equations (obviously) I would prefer them.
I hope I have explained my problem well enough.
So I have two rectangles, on a collision course. Their height, width , and y positions are exactly the same, the only thing that changes is their x position. I define the change in position for the left rectangle as 3 pixels (arbitrary number) added to x every frame of the animation. The left rectangle moves along to the right while the right rectangle is stationary (for the purpose of the logic, the right rectangle doesn't need to be moving). I know how basic collision detections works. Say we have a point defined by x + 50, which pertains to the top left corner of the left rectangle plus 50 for the width of that same rectangle. So i can write something such as if(x + 50 == top left corner of stationary rectangle). This is all fine and dandy BUT what happens if the top left corner of my rectangle is 51 pixels away from the top left corner of my right rectangle? so the change in x for the collision to occur would have to be 1 pixel for the test to check true. However my change in x is defined as 3 pixels... so on the next frame my top left corner of the left rectangle is 48 pixels away from the top right corner of the right rectangle. The collision test failed and now since my rectangle is 50 pixels wide, it is now penetrating the right rectangle and just decides to keep on moving every frame. I know simple solutions can be made such as if(x + 50 >= top left corner of right rect), blah blah. These are solutions I do not want. I want pixel perfect collision AND I also want the drawing of the rectangle to be fluent through the animation.
What I have tried so far that has seen the most success is adding my velocity vector (3) to x + 50 and then seeing if that pokes through the top left corner of the right rectangle (if(x + 50 + dx > top left corner of right rect)), THEN iterating through all the possible values of dx for(i = 0; i < dx; i++) --> if(x + 50 + i == top left corner right rect) then the change in x for that frame is equal to i. This makes it so that the left box arrives at the exact position of the right rectangle for the next frame and regular detection prevails. However the problem with this is that it changes the speed of my velocity vector for that frame, which is an incorrect solution.
I know I am missing something and have been doing a lot of reading online about collision detection and animation, but those articles seem to fail to provide me with the information I seek. I do not care to know how collisions are detected, but rather how animations are redrawn so pixel perfect detection can be made. I will let the actual detecting be a surprise when I actually care to learn those advanced methods.
PS. my frame rate is 60 fps. I have experimented with changing frame rates in accordance to the velocity vectors. However this is also to no avail. Eventually, this all will be integrated into a simple gravity, collision detection physics engine over the long haul. If such solutions involve calculus or physics equations (obviously) I would prefer them.
I hope I have explained my problem well enough.