Its been awhile since I last posted, and I did a ton of research trying to get this right. And quite frankly theres very little information out there on this, and to a degree, even some misinformation. In fact I have yet to find a correct Sweep AABB 2D article that not only shows step by step of what the code does, which they do, but show what to do with the information you have rather than just give you a class and be like, "here ya go! Good luck to ya!" God I hate that.
For example. Take this gamedev article:
Its a good article but has flaws. For starters, only one object can move. The other has to be a non moving object such as a wall. But for a game like pong, this will only work half the time, as long as your paddle is not moving since the ball is. Another thing is that both objects have to be the same size in order for that to work. I did a small demonstration by creating a stupid pong simulation, only instead of a paddle, I created a giant box thats 100x100 pixels in size, with the ball being a square 20x20 pixels in size. It fails because of this:
xInvEntry = b2.x - (b1.x + b1.w)
And lets say b1 is the Paddle, and b2 is the ball. Bare in mind, both objects are different sizes. I made the paddle approach a non moving ball and the xInvEntry went negative when it went close to the ball with a nice gap between rather than when it passes its touching point. But if both were the same width, it would do as it was intended and go negative once it passes its touching point. Another thing that bothered me was what he did with the information he got from his code:
float entryTime = std::max(xEntry, yEntry);
float exitTime = std::min(xExit, yExit);
This to me doesnt make any sense. Now I ran into another article over here:
And ok, thats better. The biggest difference between the 2 is that this article allows both objects to move by simple Vector subtraction: V = VA - VB, and both objects can be different sizes, only this time the entry and exits works with half widths and half heights for both objects: DX = (B.x - HS.x) - (A.x + HS.x), which to me is a huge leap from the other article I say. So now I have this code to work with:
Public Function Sweep_Collision_Detection(Obj1 As Sprite_Type, Obj2 As Sprite_Type) As Single Dim Velocity1 As D3DVECTOR2 Dim Velocity2 As D3DVECTOR2 Dim Velocity As D3DVECTOR2 Dim Collision_Entry As D3DVECTOR2 Dim Collision_Exit As D3DVECTOR2 Dim Entry_Time As D3DVECTOR2 Dim Exit_Time As D3DVECTOR2 Velocity1 = Vector_Subtract(Obj1.Position, Obj1.Old_Position) Velocity2 = Vector_Subtract(Obj2.Position, Obj2.Old_Position) Velocity = Vector_Subtract(Velocity1, Velocity2) If Velocity.X > 0 Then Collision_Entry.X = (Obj2.Position.X - Obj2.Half_Width) - (Obj1.Position.X + Obj1.Half_Width) Collision_Exit.X = (Obj2.Position.X + Obj2.Half_Width) - (Obj1.Position.X - Obj1.Half_Width) Entry_Time.X = (Collision_Entry.X / Velocity.X) Exit_Time.X = (Collision_Exit.X / Velocity.X) ElseIf Velocity.X < 0 Then Collision_Entry.X = (Obj2.Position.X + Obj2.Half_Width) - (Obj1.Position.X - Obj1.Half_Width) Collision_Exit.X = (Obj2.Position.X - Obj2.Half_Width) - (Obj1.Position.X + Obj1.Half_Width) Entry_Time.X = (Collision_Entry.X / Velocity.X) Exit_Time.X = (Collision_Exit.X / Velocity.X) End If If Velocity.Y > 0 Then Collision_Entry.Y = (Obj2.Position.Y - Obj2.Half_Height) - (Obj1.Position.Y + Obj1.Half_Height) Collision_Exit.Y = (Obj2.Position.Y + Obj2.Half_Height) - (Obj1.Position.Y - Obj1.Half_Height) Entry_Time.Y = (Collision_Entry.Y / Velocity.Y) Exit_Time.Y = (Collision_Exit.Y / Velocity.Y) ElseIf Velocity.Y < 0 Then Collision_Entry.Y = (Obj2.Position.Y + Obj2.Half_Height) - (Obj1.Position.Y - Obj1.Half_Height) Collision_Exit.Y = (Obj2.Position.Y - Obj2.Half_Height) - (Obj1.Position.Y + Obj1.Half_Height) Entry_Time.Y = (Collision_Entry.Y / Velocity.Y) Exit_Time.Y = (Collision_Exit.Y / Velocity.Y) End If End Function
Bare in mind this is an incomplete example. The only problem is, I have no freaking clue what to do with this information. The code is correct at least but heres the deal. Lets say I do check if it passes the other object cause it moved so fast and this Sweep collision found it passed through it. After checking it did, I changed the position of the object like the article says to where it was suppose to collide. Problem is when I moved away from the ball after it displaced the paddle, it put the freaking paddle on the other side of the ball. As though the freaking thing was glued to it, and it was moving around it while stuck. Like I said I have no clue what to do with the Entry Times and Exit Times in this code. I see people doing different things with it like checking if its larger than 0, or less than 1, or even something stupid like checking if its greater or less than the Y Entry and Exit Times. And even then, how can I even setup the if statements with the information left over? Whats the correct way of doing this? If the article had a beautiful elegant simplified example, then just maybe I could have had a general idea. So for the sake of a pong collision style example, how can I correctly implement Sweep AABB for fast objects in 2D? I hope I was clear, and yes I saw other articles, but these seemed to have been the most clear. Thanks in advance.