Simple Code to Extend Collision Range by Virtual Nomad12th Jan 2006 0:12
|
---|
Summary some very basic code here to achieve 2 levels of collision for the same player' object. Description EXTENDED COLLISION by Virtual Nomad (jan. 11, 2006) Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com rem * "EXTENDED COLLISION" by Virtual Nomad (jan. 11, 2006) rem * some very basic code here to achieve 2 levels of collision for the same rem * 'player' object. rem * rem * some notes for new programmers, like myself: rem * rem * the 'player' object (100) uses standard collision checks to keep it from rem * passing through the 'obstacles' (cubes). i used the 2nd sphere (object 200) rem * to check for collision at a range beyond the actual dimensions of the player, rem * calling this 'extended collision'. in the example here, the player takes 'damage' when rem * an obstacle is within the extended collision range of the player, as if standing too rem * close to an intense heat source. rem * rem * again, i'm a new programmer, so i'm sure there are other more efficient ways to rem * realize the concept here, but i do hope this helps other beginners visualize the rem * idea. sync on : sync rate 60 rem this is just for perspective - no collision checks are made vs matrix/terrain make matrix 10,100,100,10,10 set matrix wireframe on 10 position matrix 10,0,0,0 rem make player object make object sphere 100,10 position object 100,80,5,20 set object collision on 100 health = 1000 rem set extended collision for player using separate object make object sphere 200,30 `<<<<<< set this to desired (extended) range <<<<< position object 200,80,5,20 color object 200,rgb(80,80,80) set object collision on 200 ghost object on 200 `ghosted so you can still see the player. plus i think it looks cool :) hide object 200 `completely hidden until collision is detected. you dont ever have to show `the second sphere, tho. collision is still detected. just showed it here `so you can literally SEE what's going on. rem make obstacles - 3 cubes of doom!. for x = 25 to 75 step 25 make object cube x,10 position object x,x,5,x set object collision on x next x rem set camera - always helps when you can see things, eh? position camera 0,50,100,-25 pitch camera down 0,45 do `start the action! px = object position x (100) py = object position y (100) pz = object position z (100) color object 100,rgb(0,255,0) `set player to 'safe' color while no collision is detected hide object 200 `hide extended collision object while no collision is detected rem move player and extended collision object together (sorta/pretty much) for duo = 100 to 200 step 100 if upkey() and pz < 100 then position object duo,px,py,pz+1 if downkey() and pz > 0 then position object duo,px,py,pz-1 if leftkey() and px > 0 then position object duo,px-1,py,pz if rightkey() and px < 100 then position object duo,px+1,py,pz if spacekey() position object 100,80,5,20 position object 200,80,5,20 health = 1000 `ahh... that feels much better! endif next duo rem check for 'extended collision' using object 200 for close = 25 to 75 step 25 if object hit (200,close) = 1 color object 100,rgb(255,255,0) show object 200 `you're too close! health = health - 1 `it burns! endif next close rem check for 'direct' player collision using object 100 for boom = 25 to 75 step 25 if object hit (100,boom) = 1 color object 100, rgb(255,0,0) position object 100,oldpx,oldpy,oldpz endif next boom rem establish last 'safe' position oldpx = object position x (100) oldpy = object position y (100) oldpz = object position z (100) rem print some pertinents set cursor 0,0 print screen fps() print "player: ",px,", ",pz print "health: ",health print print "<arrow keys> to move, <space> to reset" sync `refresh screen loop `do it all over again |