Hi everyone. I've pretty much studied this dll like I was majoring in Collision Detection. Thanks to
Aralox many commands started to make sense. The problems arise when I:
1) make the movement into a function, needed in order to run smoothly with the many other mandatory features of a game.
2) walk into my other models while testing; the player bounces everywhere with no smoothing (I'm referring to the model itself, not the camera).
Not only that, when my character jumps, he is forcefully put into the falling position that also seems to be extremely slow for some reason (download the file for more information).
" if player_state=0 then loop object 1,1,1
if player_state=1 then loop object 1,2,17" is completely ignored as well.
Here the code of the function:
function control_player()
`if PlayerCollision = 0 then goto player_movement_setup
`gosub player_movement_setup
REM If player is moving, animate it
if player_state=0 then loop object 1,1,1
if player_state=1 then loop object 1,2,17
oldx# = object position x(1)
oldy# = object position y(1)
oldz# = object position z(1)
angy# = object angle y(1)
vx# = 0
vz# = 0
if vy#=0 and jumptimer=0 then vy# = vy# + 10*gravity# else vy# = vy# + gravity#
rem Move player (eight-directional movement)
If Downkey()=1 AND Leftkey()=1
yrotate object 1,45
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Downkey()=1 AND Rightkey()=1
yrotate object 1,-45
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Upkey()=1 AND Leftkey()=1
yrotate object 1,135
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Upkey()=1 AND Rightkey()=1
yrotate object 1,225
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Upkey()=1
yrotate object 1,180
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Downkey()=1
yrotate object 1,360
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Leftkey()=1
yrotate object 1,90
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
If Rightkey()=1
yrotate object 1,270
vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
player_state=1
else
player_state=0
endif : endif : endif: endif : endif : endif : endif : endif
rem END directional movement----------------------------------------------------
rem only jump if on ground, and a certain time after last jump
if ground=1
if spacekey()=1 and jumptimer=0 then vy# = vy# + 3.0 : jumptimer = 20
endif
x# = oldx#+vx#
y# = oldy#+vy#
z# = oldz#+vz#
REM The headache: checks collision via SphereCastGroup
collide = sc_SphereCastGroup(1,oldx#,oldy#,oldz#,oldx#,oldy#+vy#,oldz#,radius#,0)
collide1 = sc_SphereCastGroup(1,oldx#,oldy#+10.0,oldz#,oldx#,oldy#+vy#+10.0,oldz#,radius#,0)
if collide OR collide1>0
ny# = sc_getCollisionNormalY()
if abs(ny#)>slope#
rem FLAT, stick
oldy# = sc_getStaticCollisionY()
else
rem STEEP, slide
x# = x# - oldx# : z# = z# - oldz#
oldx# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
oldz# = sc_getCollisionSlideZ()
x# = x# + oldx# : z# = z# + oldz#
endif
if ny#>slope#
rem only on ground if standing on flat ground
ground = 1
vy# = 0
else
ground = 0
rem if player has hit a flat ceiling then stop vy# movement
if ny#<-slope# then vy# = gravity#
endif
else
rem nothing below player, not on ground, add vertical speed to player
oldy# = oldy# + vy#
ground = 0
endif
if ground = 1 and jumptimer>0 then dec jumptimer
collide = sc_SphereSlideGroup(1,oldx#,oldy#,oldz#,x#,oldy#,z#,radius#,0)
collide1 = sc_SphereSlideGroup(1,oldx#,oldy#+10.0,oldz#,x#,oldy#+10.0,z#,radius#,0)
if collide OR collide1>0
rem if hit, reposition player, halt movement vector
x# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
z# = sc_getCollisionSlideZ()
vx# = 0
vz# = 0
endif
rem position the player
position object 1,x#,oldy#,z#
sc_updateObject 1
`player_movement_setup:
`return
endfunction
REM END-----------------------------------------------------------------------------
Here are a couple startup functions from the main file:
function load_data()
rem Global data
global ambience# as integer
global blockobj as integer
dim objtype(10000)
global message_disply = 0
global returnkeyflag as integer : returnkeyflag = 0
global DIM player_distance(50)
REM Collision Global Variables
These are here so that they'll work inside the function without resetting themselves every time the program loops.
| | | | |
v v v v v
global jumptimer AS integer : jumptimer = 0
global radius# AS float: radius# = 5.0
global gravity# AS float : gravity# = -0.1
global slope# AS float : slope# = 0.9
global ground AS INTEGER : ground = 1
global player_state AS INTEGER : player_state=0
rem Load all images being used
Load Image "MediaToa_mate.png",9
load image "MediaHUD.png",11
load image "MediaMessage_Box.png",12
` load base and detail texture
load image "Mediatexture1.bmp", 13
load image "Mediadetail.tga", 14
load image "Mediawater_deep.png",15
`Font
SET TEXT FONT "Verdana"
SET TEXT SIZE 16
SET TEXT TO BOLD
`This sets up the variables for the Game Timer
global movespeed = 1060
global turnspeed = 87
time=timer()
global menuOn = 0
global mtimer = 0
`Load the data of Rahi and other enemies
global Kofo_Jaga AS INTEGER : Kofo_Jaga = 3
endfunction
function load_models()
`Matoran
KafuaMDL = 301 : load object "MediaMatoran.x",KafuaMDL
POSITION OBJECT KafuaMDL, 2297, 45, 1860
xrotate object KafuaMDL,270
fix object pivot KafuaMDL
rotate object KafuaMDL,0,180,0
scale object KafuaMDL, 400,400,400
set object light KafuaMDL,0
SC_SetupObject KafuaMDL,1,2
`Make Object Sphere 1, radius#*2.0
load object "MediaToa_Mata.x",1
POSITION OBJECT 1, 2297,45,1886
xrotate object 1,270
scale object 1,75, 75, 75
SET OBJECT SPEED 1, 60
Texture object 1,9
SC_SetupObject 1,0,1
FIX OBJECT PIVOT 1
remstart Skysphere
load object "Mediasky_sphere.x",3
set object light 3,0
scale object 3, 20000,20000,10000
set object cull 3,0
texture object 3,10 remend
rem Terrain Western Region
make object terrain 2 ` create the terrain object
set terrain heightmap 2, "Mediatest1.bmp" ` set the heightmap
set terrain scale 2, 6, 6, 6 ` set the scale
set terrain split 2, 16 ` split value by 16 * 16
set terrain tiling 2, 4 ` detail map tiling
set terrain light 2,0.6,0.6,0.6,1.0,1.0,0.9,0.9 ` light - xdir, ydir, zdir, red, green, blue, intensity
set terrain texture 2, 13, 14 ` base and detail texture
build terrain 2
`setup collision on the terrain
SC_SetupComplexObject 2,1,0
`Set up the Matrix
`make matrix 1,10000,10000,25,25
rem Ocean
load object "Mediaslab10.X",5
set object transparency 5,2
scale object 5,1000,1000,1000
load effect "MediaPocean.fx",1,1
set object effect 5,1
position object 5, 1000,10,1000
SC_setupObject 5 , 1 , 2
rem Tarakava model
load object "MediaTarakava.x",6
POSITION OBJECT 6, 2320,75,1939
xrotate object 6,270
scale object 6,180, 180, 180
`SC_SetupComplexObject 6,1,2
SC_SetupComplexObject 6,1,2
endfunction
And
before you go, I have one more question:
How does collision work for other models that move that are controlled by the computer? Do have to use the same commands for them as for the player (cause that would be a lot of code!)
http://www.cyangaming.net/downloads2/Test4.zip
I'm taking a break from AI and Collision,
Currently working on - Ring Menu