Before I say anything, I need to say that I didn't make this! I can't remember who did, but if you give me credit I will ummm, annoy you
Ok, since alot of DBC users have been looking for collisions, and I mean
alot, I'm posting this that I found somewhere. If you were the person that made this please tell me, that way everyone can know who you are..
Ok, I feel kinda silly saying this.. but I think this should be a sticky, the sticky to put an end to "I need help with collision".. sp pleaasssee sticky this, it would be so much help to all the DBC users who can make their own collision
Code: i didnt really make this, I just cleaned it up so it suited me better
`Setup
Sync on
Sync rate 0
Hide mouse
gmspd#=1.0
set camera range 1,99999
`Variables and Arrays
charht#=50
spd#=10
radius#=50.0
checks=8
if checks<4 then checks=4
if checks>360 then checks=360
angwidth=360/checks
dim ang#(checks-1,2)
dim sort#(checks-1,2)
`Create Place
load image "texture.jpg",50
load object "landscape.x",50
scale object 50,5000,5000,5000
position object 50,0,1000,0
texture object 50,50
setupComplexObjectDBC(50,1,2)
updateObjectDBC(50)
position camera 0,6000,0
`Stuff
dir$=get dir$()
`))Main Loop((
Do
text 10,10,"FPS: "+str$(screen fps())
center text screen width()/2,10,"Use mouse to look, W,A,S,D keys to move"
center text screen width()/2,30,"Press space to jump"
`Camera rotation
oldx#=camera position x()
oldz#=camera position z()
cx#=camera position x()
cy#=camera position y()
cz#=camera position z()
ca#=camera angle y()
ax#=wrapvalue(ax#+mousemovey()*0.3)
ay#=wrapvalue(ay#+mousemovex()*0.3)
if ax#<=290 and ax#>180 then ax#=290
if ax#>=70 and ax#<=180 then ax#=70
acx#=curveangle(ax#,acx#,20.0)
acy#=curveangle(ay#,acy#,20.0)
xrotate camera acx#
yrotate camera acy#
`keyboard controls
`WASD
if keystate(17)
cx# = newxvalue(cx#,ca#,spd#)
cz# = newzvalue(cz#,ca#,spd#)
endif
if keystate(31)
cx# = newxvalue(cx#,ca#,-1*spd#)
cz# = newzvalue(cz#,ca#,-1*spd#)
endif
if keystate(30)
cx# = newxvalue(cx#,wrapvalue(ca#-90.0),spd#)
cz# = newzvalue(cz#,wrapvalue(ca#-90.0),spd#)
endif
if keystate(32)
cx# = newxvalue(cx#,wrapvalue(ca#+90.0),spd#)
cz# = newzvalue(cz#,wrapvalue(ca#+90.0),spd#)
endif
if spacekey()=1 and jump#=0 then jump#=20
if spacekey()=0 then jump#=0
cy#=cy#+jump#
position camera cx#,cy#,cz#
`position light 0,cx#,cy#,cz#
`*Collsion*
gosub _collision
gosub _ramp
`))End Loop((
Sync
Loop
_ramp:
`get positions again
cx#=camera position x()
cz#=camera position z()
`Get your old height
oldht#=camera position y()
`calc new height using intersect object from the old height down charht# units
col=intersectObjectDBC(1,1,cx#,oldht#+10,cz#,cx#,oldht#-(charht#*2),cz#,0)
if col=0 Then sub#=0 Else sub#=abs(oldht#-getStaticCollisionY())
ht#=(oldht#+charht#)-(sub#/2)
if sub#=0 then ht#=charht#
`do some gravity
grav#=grav#-0.5
`if you are going up a ramp
if oldht#+grav#<ht#
grav#=0.0
`next line makes stairs climbing more natural
hup# = curvevalue(oldht#,ht#,1.5)
position camera cx#,hup#,cz#
else
`if you are falling
ht#=oldht#+grav#
position camera cx#,ht#,cz#
endif
return
_collision:
for x=0 to checks-1
chx#=newxvalue(cx#,x*angwidth,100)
chz#=newzvalue(cz#,x*angwidth,100)
col=intersectObjectDBC(1,1,cx#,cy#,cz#,chx#,cy#,chz#,0)
If col=0 Then ang#(x,1)=0 Else ang#(x,1)=sqrt((getStaticCollisionX()-cx#)^2+(getStaticCollisionZ()-cz#)^2)
if ang#(x,1)=0 then ang#(x,1)=999999
ang#(x,2)=x*angwidth
sort#(x,1)=ang#(x,1)
sort#(x,2)=ang#(x,2)
next x
`sort the array to find the closest object and it's degrees
for x=0 to checks-2
for y=x+1 to checks-1
if ang#(x,1)>ang#(y,1)
temp#=ang#(x,1)
temp2#=ang#(x,2)
ang#(x,1)=ang#(y,1)
ang#(x,2)=ang#(y,2)
ang#(y,1)=temp#
ang#(y,2)=temp2#
endif
next x
next y
`This is the closest wall and what degrees you would face to see it
print "Angle to closest wall:",ang#(0,2)
print "Distance to that wall:",ang#(0,1)
`find +-90 degrees from the closest one for when you walk into a corner so it doesn't shake
prev=wrapvalue(ang#(0,2)-90)/angwidth
nxt=wrapvalue(ang#(0,2)+90)/angwidth
newd#=radius#-ang#(0,1)
newa#=wrapvalue(ang#(0,2)-180)
newd1#=radius#-sort#(prev,1)
newa1#=wrapvalue(sort#(prev,2)-180)
newd2#=radius#-sort#(nxt,1)
newa2#=wrapvalue(sort#(nxt,2)-180)
`if you are less than radius from a wall, push you out to 20 from it
if ang#(0,1)<radius# and ang#(0,1)>0.0
repx#=newxvalue(cx#,newa#,newd#)
repz#=newzvalue(cz#,newa#,newd#)
position camera repx#,cy#,repz#
endif
cx#=camera position x()
cz#=camera position z()
`this is if you are coming into a corner, this pushes you sideways
if sort#(prev,1)<radius# and sort#(prev,1)>0.0
repx1#=newxvalue(cx#,newa1#,newd1#)
repz1#=newzvalue(cz#,newa1#,newd1#)
position camera repx1#,cy#,repz1#
endif
cx#=camera position x()
cz#=camera position z()
`and the other way. above is left, this is right
if sort#(nxt,1)<radius# and sort#(nxt,1)>0.0
repx2#=newxvalue(cx#,newa2#,newd2#)
repz2#=newzvalue(cz#,newa2#,newd2#)
position camera repx2#,cy#,repz2#
endif
return
`***Collision stuff****
function setupObjectDBC(objNum,groupNum,objectType)
commonSetup(objNum)
vertData = get memblock ptr(254)
objectData = get memblock ptr(255)
call dll 1,"setupObject",objNum,groupNum,objectType,vertData,objectData
delete memblock 254
endfunction
function setupComplexObjectDBC(objNum,groupNum,facesPerNode)
commonSetup(objNum)
vertData = get memblock ptr(254)
objectData = get memblock ptr(255)
call dll 1,"setupComplexObject",objNum,groupNum,facesPerNode,vertData,objectData
delete memblock 254
endfunction
function commonSetup(objNum)
if dll exist(1)=0 then load dll "DBCcollision.dll",1
if memblock exist(255)=0 then make memblock 255,24
x#=object position x(objNum)
y#=object position y(objNum)
z#=object position z(objNum)
angx#=object angle x(objNum)
angy#=object angle y(objNum)
angz#=object angle z(objNum)
write memblock float 255,0,x#
write memblock float 255,4,y#
write memblock float 255,8,z#
write memblock float 255,12,angx#
write memblock float 255,16,angy#
write memblock float 255,20,angz#
position object objNum,0,0,0
rotate object objNum,0,0,0
make mesh from object 255,objNum
make memblock from mesh 254,255
delete mesh 255
position object objNum,x#,y#,z#
rotate object objNum,angx#,angy#,angz#
endfunction
function updateObjectDBC(objNum)
if memblock exist(255)=0 then make memblock 255,24
write memblock float 255,0,object position x(objNum)
write memblock float 255,4,object position y(objNum)
write memblock float 255,8,object position z(objNum)
write memblock float 255,12,object angle x(objNum)
write memblock float 255,16,object angle y(objNum)
write memblock float 255,20,object angle z(objNum)
objectData = get memblock ptr(255)
call dll 1,"updateObject",objNum,objectData
endfunction
function intersectObjectDBC(objNum,groupFlag,oldx#,oldy#,oldz#,x#,y#,z#,excludeObj)
collide=call dll(1,"intersectObject",objNum,groupFlag,oldx#,oldy#,oldz#,x#,y#,z#,excludeObj)
endfunction collide
function setObjectCollisionOnDBC(objNum)
call dll 1,"set_object_collision_on",objNum
endfunction
function setObjectCollisionOffDBC(objNum)
call dll 1,"set_object_collision_off",objNum
endfunction
function collisionStatusDBC(objNum)
result=call dll(1,"collisionstatus",objNum)
endfunction result
function getStaticCollisionX()
result#=call dll(1,"getStaticCollisionX")
endfunction result#
function getStaticCollisionY()
result#=call dll(1,"getStaticCollisionY")
endfunction result#
function getStaticCollisionZ()
result#=call dll(1,"getStaticCollisionZ")
endfunction result#
function getCollisionNormalX()
result#=call dll(1,"getCollisionNormalX")
endfunction result#
function getCollisionNormalY()
result#=call dll(1,"getCollisionNormalY")
endfunction result#
function getCollisionNormalZ()
result#=call dll(1,"getCollisionNormalZ")
Media is attached..
Now you DBC users can all make a game with collision (unless your already seen this code somewhere before..)