Posted: 23rd Apr 2004 3:36
This code will show you how to code Y-rotated box collision. It's a method im still working on, but it works

+ Code Snippet
remstart
--------------------------------------------------------------

                    Rotated Box Collision
                      by Emperor-Baal

note:
-This method is not finished, im still tweaking it, but it works.

 It also works with sliding collision, which is explained in my tutorial:
 http://www.geocities.com/hatesurvivor/MathColl.htm

-This method only works with Y-rotated boxes (x and z size are the same)

-You may alter my code if you want. If you find a better way to check for
 collision with rotated boxes then email me at once ;)
 geile_teddybeer@msn.com
-------------------------------------------------------------
remend


rem Use this line to call the function

rem Rotated_Box_Collision( moving object number,  hard object number, hard object size / 1.414)



rem Sets the refresh rate to the maximum
sync on
sync rate 0


rem Lets make the cube that's hard
make object cube 2,10

rem Positions the cube at the center
position object 2,0,0,0


rem Lets make 291 spheres that will move towards the cube
for a=10 to 300
 make object sphere a,0.4
 position object a,rnd(100)-50,0,rnd(100)-50
 point object a,0,0,0
next a





rem Start of the main-loop
do

rem Check for collision with the spheres, if there's no collision then move the sphere closer.
for a=10 to 300

 rem Check if collision is possible, using a position check
 if object position y(a) > object position y(2)-(object size y(2)/2.0)
  if object position y(a) < object position y(2)+(object size y(2)/2.0)
      rem rotated_box_collision(moving object, hard object, size of object 2 / 1.414
      if rotated_box_collision(a,2,object size x(2)/1.414) = 0 then move object a,0.05
  endif
 endif

next a


rem If the user presses the spacekey, the cube will rotate and every sphere that collides with the cube is repositioned to a random location
if spacekey()=1
 yrotate object 2,wrapvalue(object angle y(2)+0.2)
 for a=10 to 300
  if rotated_box_collision(a,2,10.0) = 1
  position object a,rnd(100)-50,0,rnd(100)-50
  point object a,0,0,0
  endif
 next a
endif



position camera 10,30,0
point camera 0, 0,0


sync
loop








function rotated_box_collision(a,b,size#)

rem Get some position values
x# = object position x(a)
y# = object position y(a)
z# = object position z(a)

x2# = object position x(b)
y2# = object position y(b)
z2# = object position z(b)

rem Get the angle for the distance check
ya# = wrapvalue(360-object angle y(b)) + 45.0 + atanfull(x#-x2#,z#-z2#)

rem 0.95 is the magical tweak number, try changing it by steps of 0.1
fla# = (0.95 - cos(45)) / 45.0


rem ---------------------------------------------------


rem Make sure it lies between 0.0 and 90.0
while ya# > 90.0
 ya# = ya# - 90.0
endwhile

while ya# < 0.0
 ya# = ya# + 90.0
endwhile


rem ---------------------------------------------------


rem This will try to get "real" distance to the cube
if ya# <= 45.0
 yb# = 1.0 - (ya# * fla#)
 rem Returns a value between 1.0 and 0.7
else
 yb# = 1.0 - ((45.0 - (ya# - 45.0))*fla#)
 rem Returns a value between 0.7 and 1.0
endif


rem ---------------------------------------------------


rem Reset the result
result = 0

rem Final Distance
superim# = size# * yb#

rem Ordinary distance check using the freshly calculated distance
if SQRT( (x#-x2#)^2 + (z#-z2#)^2 ) < superim# then result = 1
endfunction result