TGC Codebase Backup



Collision against multiple objects by Image All

5th Mar 2006 19:20
Summary

This demonstrates collision on multiple objects.



Description

This is a medialess example of how to quickly and efficiently perform collision agains multiple object, e.g. when a dummy object (wooden crate or barrel) hits another dummy object. This is an almost-accurate way of doing things, but in later versions I will add rotation e.g. the boxes will rotate when they are pressed at any corner, and hence, will have more of a realistic effect.

W to move forward
S to move backward
Mouse to look



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on : sync rate 60 : hide mouse
autocam off

center text screen width()/2,screen height()/2,"Loading.."
sync : sync

make object cube 1,5
make object cube 2,5
make object cube 3,5
make object cube 4,5
position object 1,-7.25,0,7.25
position object 2,7.25,0,7.25
position object 3,-7.25,0,-7.25
position object 4,7.25,0,-7.25
set object collision to boxes 1
set object collision to boxes 2
set object collision to boxes 3
set object collision to boxes 4

make object cube 5,5
position object 5,0,0,0
color object 5,rgb(0,200,0)
set object collision to boxes 5

global Plrx#=0.0
global Plrz#=0.0
global Plra#=0.0

do
   if keystate(17)=1 then inc Plrx#,sin(Plra#)*0.2 : inc Plrz#,cos(Plra#)*0.2
   if keystate(31)=1 then dec Plrx#,sin(Plra#)*0.2 : dec Plrz#,cos(Plra#)*0.2
   Plra# = wrapvalue(Plra#+(mousemovex()*0.2))

   for i=1 to 4
      if object collision(i,0)<>0 then CollideObjects(object collision(i,0),i)
   next i

   position object 5,Plrx#,0,Plrz#
   rotate object 5,0,Plra#,0
   position camera (Plrx#-(sin(Plra#)*15)),10,(Plrz#-(cos(Plra#)*15))
   point camera Plrx#,0,Plrz#

   sync
loop

function CollideObjects(Pl,Df)
   Dfx#=object position x(Df)
   Dfz#=object position z(Df)
   Dfsx#=object size x(Df)/2
   Dfsz#=object size z(Df)/2
   left#=intersect object(Pl,Dfx#-Dfsx#,0,Dfz#-Dfsz#,Dfx#-Dfsx#,0,Dfz#+Dfsz#)
   top#=intersect object(Pl,Dfx#-Dfsx#,0,Dfz#+Dfsz#,Dfx#+Dfsx#,0,Dfz#+Dfsx#)
   right#=intersect object(Pl,Dfx#+Dfsx#,0,Dfz#+Dfsz#,Dfx#+Dfsx#,0,Dfz#-Dfsz#)
   bottom#=intersect object(Pl,Dfx#-Dfsx#,0,Dfz#-Dfsz#,Dfx#+Dfsx#,0,Dfz#-Dfsz#)
   if left# > 0 then move object right Df,0.2
   if top# > 0 then move object Df,-0.2
   if right# > 0 then move object left Df,0.2
   if bottom# > 0 then move object Df,0.2
endfunction