Collision issues

Blitz3D Forums/Blitz3D Beginners Area/Collision issues

zortzblatz(Posted 2008) [#1]
I have no idea what is wrong with my code. I want the cans of lazer to disappear upon colliding with the camera. I cannot figure out why they aren't
<code>

; ---------------------------------------------------------------
; Graphics/light...
; ---------------------------------------------------------------

Global dWidth = 640
Global dHeight = 480

Graphics3D dWidth, dHeight
SetBuffer BackBuffer ()


run = LoadSound ("snd/gravel.wav")
wade = LoadSound ("snd/water2.wav")




Type Timer
Field start
Field timeOut
End Type

;------------------------------------------------------------------
;and ZortZblatZ said, let there be cans!
;------------------------------------------------------------------



Type lazercan
Field model
Field ID
Field collided
End Type





;-------------------------------------------------------------------
;creates the ship
;-------------------------------------------------------------------

shipmodel = LoadMesh ("msh/ship3.3ds")
EntityShininess shipmodel, 0.1
ship = CenterPivot (shipmodel)
ScaleEntity ship, 6, 6, 6
PositionEntity ship, 3750, 600, 1300
RotateEntity ship, 0, 45, 0
scaler = 2


terrain = LoadTerrain ("gfx/height.bmp")
ScaleEntity terrain, 5, 50, 5
TerrainShading terrain, True
TerrainDetail terrain, 2500, True
grass = LoadTexture ("gfx/sand.jpg")
ScaleTexture grass, 20, 20
EntityTexture terrain, grass, 0, 1
MoveEntity terrain, 0, -4, 0

seabed = CreatePlane ()
EntityTexture seabed, grass
MoveEntity seabed, 0, -3.9, 0



; ---------------------------------------------------------------
; Water...
; ---------------------------------------------------------------

water = CreatePlane ()
h20 = LoadTexture ("gfx/greywater.bmp")
EntityAlpha water, 0.75
PositionEntity water, 0, -25, 0
ScaleTexture h20, 200, 200
EntityTexture water, h20
EntityColor water, 64, 64, 64
EntityShininess water, 0.05

; ---------------------------------------------------------------
; Camera and fog...
; ---------------------------------------------------------------

cam = CreatePivot ()
EntityRadius cam, 2

camera = CreateCamera (cam)
CameraViewport camera, 0, 0, dWidth, dHeight
CameraFogMode camera, 1
CameraFogRange camera, 1, 1600
CameraFogColor camera, 0, 0, 0
CameraRange camera, 1, 1600 * scaler
PositionEntity cam, 2340, 0, 2390
dome = CreateSphere (12)
clouds = LoadTexture ("gfx/sky.bmp")
ScaleEntity dome, 1600 * scaler, 1600 * scaler, 1600 * scaler
EntityTexture dome, clouds
ScaleTexture clouds, 0.25, 0.25
EntityOrder dome, 1
FlipMesh dome
EntityFX dome, 8

flash = CreateLight ()
LightColor flash, 0, 0, 0
PositionEntity flash, 1900, 100, 0
RotateEntity flash, 90, 0, 0

; ---------------------------------------------------------------
; Collisions...
; ---------------------------------------------------------------

;Const ENTITY_TERRAIN = 1
;Const ENTITY_CAM = 2
;Const ENTITY_PLANE = 3
;Const ENTITY_CAN = 4
Global ENTITY_TERRAIN = 1
Global ENTITY_CAM = 2
Global ENTITY_PLANE = 3
Global ENTITY_CAN = 4

EntityType terrain, ENTITY_TERRAIN
EntityType cam, ENTITY_CAM
;If gimme747
EntityType shipmodel, ENTITY_PLANE
;EndIf

Collisions ENTITY_CAM, ENTITY_TERRAIN, 2, 1
Collisions ENTITY_CAM, ENTITY_PLANE, 2, 1
Collisions ENTITY_CAM, ENTITY_TERRAIN, 1, 2
Collisions ENTITY_CAM, ENTITY_PLANE, 1, 2

Function CurveValue#(current#,destination#,curve)
current#=current#+((destination#-current#)/curve)
Return current#
End Function

; ---------------------------------------------------------------
; Main loop...
; ---------------------------------------------------------------


If EntityCollided (camera, ENTITY_CAN)
HideEntity lazercan
EndIf


canmodel = LoadMesh("msh\can.3ds")
ScaleEntity canmodel,5,5,5

For c=1 To 1000
canz.lazercan = New lazercan

canz\ID = CopyEntity (canmodel)
;EntityColor canz\ID,Rand(0,255),Rand(0,255),Rand(0,255)
x = Rand(1000,3000)
z = Rand(1000,3000)
PositionEntity canz\ID,x,TerrainY(terrain,x,0,z)+15,z
TurnEntity canz\ID,0,Rand(0,365),0
EntityType canz\ID, ENTITY_CAN
EntityRadius canz\ID,20
Collisions ENTITY_CAM, ENTITY_CAN,1,2
Collisions ENTITY_CAM, ENTITY_CAN,2,1

Next



FPS = 50

period = 1000 / FPS
time = MilliSecs () - period

SoundVolume run, 0
SoundVolume wade, 0


LoopSound run
LoopSound wade

runChannel = PlaySound (run)
wadeChannel = PlaySound (wade)



CameraFogColor camera, 36, 36, 36

; Titles...
horror = LoadSprite ("gfx/ambatlogo.bmp", 48, camera)
ScaleSprite horror, 9, 5
PositionEntity horror, 0, 0, 10
horrorAlpha# = 0
EntityAlpha horror, horrorAlpha
showHorror = 1

steps# = 1.5

HidePointer


disclaimer$ = "Better than Jed's version."
SetFont LoadFont ("arial")

Repeat

If startlite < 40
startlite = startlite + 0.5
;AmbientLight startlite, startlite, startlite
AmbientLight 300000,300000,300000
EndIf

If makesun = 0
sun = CreateLight ()
LightColor sun, 0, 0, 0
PositionEntity sun, 3000, 500, 3000
RotateEntity sun, 45, 0, 0
makesun = 1
Else
; Titles... requesting the services of Major Hack... Major Hack, you're needed for some titles...
If Not noMoreHorror
EntityAlpha horror, horrorAlpha
If Not reduce
If horrorAlpha <= 1
horrorAlpha = horrorAlpha + 0.05
Else
reduce = 1
EndIf
Else
If horrorAlpha => 0
horrorAlpha = horrorAlpha - 0.005
EndIf
EndIf
If horrorAlpha <= 0
FreeEntity horror
noMoreHorror = 1
EndIf
EndIf

EndIf

If sunlite < 70
sunlite = sunlite + 0.5
If sun
LightColor sun, sunlite, sunlite, sunlite
EndIf
EndIf

If foglite < 20
foglite = foglite + 0.5
CameraFogColor camera, foglite, foglite, foglite
CameraClsColor camera, foglite, foglite, foglite
EndIf

Repeat
elapsed = MilliSecs () - time
Until elapsed

ticks = elapsed / period

tween# = Float (elapsed Mod period) / Float (period)

For framelimit = 1 To ticks

If framelimit = ticks Then CaptureWorld
time = time + period

PositionEntity cam, EntityX (cam), TerrainY (terrain, EntityX (cam), EntityY (cam), EntityZ (cam)) + 25, EntityZ (cam)

mxs = MouseXSpeed()
mys = MouseYSpeed()

dest_xang# = dest_xang + mys
dest_yang# = dest_yang - mxs

xang# = CurveValue (xang, dest_xang, 5)
yang# = CurveValue (yang, dest_yang, 5)

RotateEntity camera, xang, 0, 0
RotateEntity cam, 0, yang, 0

MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

running# = 0

If KeyDown (200)
MoveEntity cam, 0, 0, steps
running = Rnd (0.35, 0.75)
EndIf

If KeyDown (208)
MoveEntity cam, 0, 0, -steps
running = Rnd (0.55, 0.95)
EndIf









If running > 0
ChannelPitch runChannel, Rnd (8000, 14000)
ChannelPitch wadeChannel, Rnd (7000, 9000)
EndIf



wading# = 0

If running
If EntityY (cam) < EntityY (water) + 24
wading = Rnd (0.5, 1)
running = 0
EndIf
EndIf

ChannelVolume wadeChannel, wading
ChannelVolume runChannel, running

PositionEntity dome, EntityX (cam), EntityY (cam), EntityZ (cam)
TurnEntity dome, 0.005, 0.025, 0.005
UpdateWorld


;---------------------------------------------------------------------------
;water movement
;---------------------------------------------------------------------------



PositionEntity water, Sin (time * 0.01) * 10, (EntityY (water) + (Sin (time * 0.05) * 0.2) * 0.25), Cos (time * 0.02) * 10

If foglite > 20
foglite = foglite - 10
If foglite < 20 Then foglite = 20
EndIf
CameraClsColor camera, foglite, foglite, foglite


Next

RenderWorld tween

If Not nomorehorror
Text (GraphicsWidth () / 2) - (StringWidth (disclaimer$) / 2), GraphicsHeight () - (StringHeight (disclaimer$) * 2), disclaimer$
EndIf



Flip


Until KeyDown (1)

Function CenterPivot (model)
FitMesh model, 0, 0, 0, MeshWidth (model), MeshHeight (model), MeshDepth (model)
modelPivot = CreatePivot ()
PositionEntity modelPivot, MeshWidth (model) / 2, MeshHeight (model) / 2, MeshDepth (model) / 2
EntityParent model, modelPivot
Return modelPivot
End Function

Function SetTimer.Timer (timeOut)
t.Timer = New Timer
t\start = MilliSecs ()
t\timeOut = t\start + timeOut
Return t
End Function

Function TimeOut (test.Timer)
If test <> Null
If test\timeOut < MilliSecs ()
Delete test
Return 1
EndIf
EndIf
End Function
End
</code>


puki(Posted 2008) [#2]
If EntityCollided (camera, ENTITY_CAN)
HideEntity lazercan
EndIf

Shouldn't that be in the actual Main Loop?


KillerX(Posted 2008) [#3]
Did you assign an entitytype to Camera?


zortzblatz(Posted 2008) [#4]
Umm, it is in the main loop (I think) I can collide with the cans but I just stop and they don't disappear.


Ross C(Posted 2008) [#5]
You need to learn to use functions better :o) If only to make the code more readable, as it's very hard to pinpoint sections of code.

I agree with puki. That code needs to be in the main loop, or it won't work at all. You need to check EVERY loop for collisions.