Hello everyone!
I am trying to make a program where you drop a ball at the top of the 3d object and it rolls down. Unfortunately, I am having problems...
First of all, here is my code:
rem setup screen
set display mode 800,600,32
sync on
sync rate 60
backdrop on
color backdrop 0
hide mouse
rem load sparky
#include "sparky.dba"
load dll "DBCcollision.dll",1
rem load a level
load object "slide.x",1
SetupComplexObjectDBC(1,1,2)
UpdateObjectDBC(1)
rem load ball
make object sphere 2,10
position object 2,20-(object size x(1)/2),object size y(1),(object size z(1)/2)-20
rem main loop
dist=200
height#=100
angle#=345
do
if spacekey()=1 then position object 2,20-(object size x(1)/2),object size y(1),(object size z(1)/2)-20:grav#=0
rem control camera
angle#=wrapvalue(angle#+mousemovex())
inc height#,mousemovey()
if upkey()=1 then dec dist,5
if downkey()=1 then inc dist,5
set camera to follow 0,0,0,angle#,dist,height#,10,0
point camera 0,0,0
rem get ball positions
x#=object position x(2)
y#=object position y(2)
z#=object position z(2)
rem gravity
dec grav#,0.1
inc y#,grav#
rem collision with ground
col=IntersectObjectDBC(1,0,x#,y#,z#,x#,y#-5,z#,2)
if col
y#=GetStaticCollisionY()+5
grav#=0
endif
rem get X heights
col=IntersectObjectDBC(1,0,x#-5,y#,z#,x#-5,y#-10,z#,2)
if col
y1#=GetStaticCollisionY()
else
y1#=-60000
endif
if y1#!-60000
col=IntersectObjectDBC(1,0,x#+5,y#,z#,x#+5,y#-10,z#,2)
if col
y2#=GetStaticCollisionY()
else
y2#=-60000
y1#=-60000
endif
endif
rem calculate slope X
if y1#!-60000
dif1#=y2#-y1#
anglex#=atanfull(10,dif1#)
endif
rem get Z heights
col=IntersectObjectDBC(1,0,x#,y#,z#-5,x#,y#-10,z#-5,2)
if col
y1#=GetStaticCollisionY()
else
y1#=-60000
endif
if y1#!-60000
col=IntersectObjectDBC(1,0,x#,y#,z#+5,x#,y#-10,z#+5,2)
if col
y2#=GetStaticCollisionY()
else
y2#=-60000
y1#=-60000
endif
endif
rem calculate slope Z
if y1#!-60000
dif2#=y2#-y1#
anglez#=atanfull(10,dif2#)
endif
rem calculate angle ball has to be rotated
angley#=atanfull(anglex#,anglez#)
rem calculate speed
dspeed#=abs((dif1#/8)+(dif2#/8))
speed#=curvevalue(dspeed#,speed#,30)
rem move ball
x#=newxvalue(x#,angley#,dspeed#)
z#=newzvalue(z#,angley#,dspeed#)
rem update object
position object 2,x#,y#,z#
rem refresh screen
sync
rem end of main loop
loop
You will need to download the attached file for the media and sparky's dll.
What my program does, is it gets the height of the ground on the left and right side of the ball, subtracts those values to get the difference, and uses
ATANFULL() to calculate the tilt of the slope on the x axis. The same with the Z axis, I get the height in front and behind the ball, subtract and use
ATANFULL() to calculate the tilt of the slope on the z axis.
Now, what I am having problems with, is to figure what Y angle the sphere has to point to move in the right direction. I thought of using
ATANFULL(anglex#,anglez#), but am not sure if that is the correct way of doing that. If you download the file, you will see that the sphere just rolls in one direction, off the side of the level and never to be seen again.
I appreciate any help or input from anyone!
TheComet
Ah yeah, for those that download it, mouse to view, up and down key to zoom and space to re-spawn the ball.
Peachy, and the Chaos of the Gems
