OK well I've got this function which finds the time of collision between two AABB's (rectangles). It seems to work. I've done my best to convert it to Blitz (I'm no expert). It returns whether a collision has occured and 'tf' contains the time of the first collision, and 'tl' the time of the last collision - the time at which the rectangles no longer collide.
Basically you multiply the x-vector and y-vector of each object with 'tf' to get the position when they first collide.
Here is the Code and functions: Blitz may have Max and Min functions but they weren't documented very well in the help section so I wrote my own.
Basically you multiply the x-vector and y-vector of each object with 'tf' to get the position when they first collide.
Here is the Code and functions: Blitz may have Max and Min functions but they weren't documented very well in the help section so I wrote my own.
Type rect Field x1#,y1#,x2#,y2# Field w#,h# Field xv#,yv# End Type Function MovingBoxColl(a:rect,b:rect,tf:Float Ptr,tl:Float Ptr) If rectsoverlap(a.x1,a.y1,a.w,a.h,b.x1,b.y1,b.w,b.h) tf[0]=0;tl[0]=0 Return True EndIf 'calculate relative velocity rvx#=b.xv-a.xv rvy#=b.yv-a.yv 'initialise points of first and last contact tf[0]=0; tl[0]=1 'for X axis determine the times of first and last contact, if any If rvx<0 If b.x2<a.x1 Then Return False ' Non-intersecting and moving apart If a.x2<b.x1 Then tf[0]=RMax((a.x2-b.x1)/rvx,tf[0]) If b.x2>a.x1 Then tl[0]=RMin((a.x1-b.x2)/rvx,tl[0]) EndIf If rvx>0 If b.x1>a.x2 Then Return False ' Non-intersecting and moving apart If b.x2<a.x1 Then tf[0]=RMax((a.x1-b.x2)/rvx,tf[0]) If a.x2>b.x1 Then tl[0]=RMin((a.x2-b.x1)/rvx,tl[0]) EndIf 'No overlap possible if time of first contact occurs after time of last contact If tf[0]>tl[0] Then Return False 'for Y axis determine the times of first and last contact, if any If rvy<0 If b.y2<a.y1 Then Return False ' Non-intersecting and moving apart If a.y2<b.y1 Then tf[0]=RMax((a.y2-b.y1)/rvy,tf[0]) If b.y2>a.y1 Then tl[0]=RMin((a.y1-b.y2)/rvy,tl[0]) EndIf If rvy>0 If b.y1>a.y2 Then Return False ' Non-intersecting and moving apart If b.y2<a.y1 Then tf[0]=RMax((a.y1-b.y2)/rvy,tf[0]) If a.y2>b.y1 Then tl[0]=RMin((a.y2-b.y1)/rvy,tl[0]) EndIf 'No overlap possible if time of first contact occurs after time of last contact If tf[0]>tl[0] Then Return False Return True End Function Function RMax:Float(a:Float,b:Float) If b>a Then Return b Return a End Function Function RMin:Float(a:Float,b:Float) If b<a Then Return b Return a End Function Function rectsoverlap:Int(x0:Int,y0:Int,w0:Int,h0:Int,x2:Int,y2:Int,w2:Int,h2:Int) If x0>(x2+w2)Or(x0+w0)<x2 Then Return False If y0>(y2+h2)Or(y0+h0)<y2 Then Return False Return True End Function