colliding copies of type?

Blitz3D Forums/Blitz3D Programming/colliding copies of type?

Lorenzo(Posted 2003) [#1]
I have an asteroid type that is copied multiple times and each copy is randomly placed in space along the XZ coodinates. They randomly move and spin. How would I (1) get each asteroid to bounce off each other and (2) the best way to get the translation movement to wrap around the screen? Thanks for anyone's assistance.
Here is my code so far:

Graphics3D 800,600,32,1
SetBuffer BackBuffer()
SeedRnd (MilliSecs())

camera=CreateCamera ()
point=CreatePivot()
mainlight=CreateLight()
LightColor mainlight,255,255,10
sublight=CreateLight()
LightColor sublight,-100,-100,-100
indlight=CreateLight()
LightColor indlight,255,2,255

PositionEntity camera,0,100,0
PositionEntity mainlight,20,5,20
PositionEntity sublight,0,5,-0
PositionEntity indlight,0,2,-100
PositionEntity point,0,0,0

PointEntity camera,point
PointEntity mainlight,point
PointEntity sublight,point
PointEntity indlight,point

Const rock_collision = 1
Const bullet_collision = 2
Const ship_collision = 3

Global bam=0
Global numshot=0
Global bop=0
Global angle#=0
Global timage=0
Global health=3
Global movex#=0
Global movez#=0

Type asteroid
Field aster
Field x#
Field y#
Field z#
Field speedx#
Field speedz#
Field spin#
Field life
Field distance#
End Type

Type player
Field entity
Field dist#
Field x#
Field y#
Field z#
End Type

Type bullet
Field entity
Field distance_to_travel#
Field x#
Field y#
Field z#
Field hit
End Type

Global rock=LoadMesh("images/rock1a.3ds")
EntityRadius rock,6
EntityType rock,rock_collision
HideEntity rock

For n=0 To 9
ast.asteroid=New asteroid
ast\aster=CopyEntity(rock)
ast\x#=Rnd(-70,70)
ast\y#=0
ast\z#=Rnd(-70,70)
ast\speedx#=Rnd(-.2,.2)
ast\speedz#=Rnd(-.2,.2)
ast\spin# = Rnd(-0.5,0.5)
ast\life=5
PositionEntity ast\aster,ast\x#,0,ast\z#
RotateEntity ast\aster,0,Rnd(0,359),0
ScaleEntity ast\aster,.5,.5,.5
Next

Global ship=LoadMesh("images/playership.3ds")
EntityRadius ship,3
EntityType ship,ship_collision
;HideEntity player
shp.player=New player
shp\entity=ship
shp\dist#=0.05
shp\x#=0
shp\y#=0
shp\z#=0
PositionEntity shp\entity,shp\x#,0,shp\z#

;-- create base bullet entity
Global bullet_entity = LoadMesh("images/bullet.x");CreateSphere(6) ;use your sprite here
EntityColor bullet_entity, 255, 0, 0
EntityRadius bullet_entity,2
EntityType bullet_entity,bullet_collision ;set collision type
HideEntity bullet_entity

;--setup sphere_to_polygon collision between rocks and bullets and ships
Collisions bullet_collision,rock_collision, 2, 1
Collisions ship_collision,rock_collision, 2, 1

;MAIN LOOP------------------------------------
While Not KeyHit(1)

playergo()

asteroidism()

MoveBullets()

UpdateWorld
RenderWorld
Text 50,50,"SCORE: "+bam
Text 50,60,"SHIP HITS: "+bop
Flip
Wend
ClearCollisions
End

;Asteroid function----------------------------
Function asteroidism()
For ast.asteroid=Each asteroid
smack=EntityCollided(ast\aster,bullet_collision)
If smack
ast\life=ast\life-1
If ast\life=0
FreeEntity ast\aster
Delete ast
bam=bam+100
EndIf
Else
TurnEntity ast\aster,ast\spin#,ast\spin#,ast\spin#
TranslateEntity ast\aster,ast\speedx#,0,ast\speedz#
If EntityZ#(ast\aster)>100
PositionEntity ast\aster,ast\x#,ast\y#,-100
EndIf
If EntityX#(ast\aster)>120
PositionEntity ast\aster,-120,ast\y#,ast\z#
EndIf
If EntityZ#(ast\aster)<-100
PositionEntity ast\aster,ast\x#,ast\y#,100
EndIf
If EntityX#(ast\aster)<-120
PositionEntity ast\aster,120,ast\y#,ast\z#
EndIf
EndIf
Next
End Function

;Player function--------------------------------
Function playergo()
For shp.player=Each player
bop=EntityCollided(shp\entity,rock_collision)
If bop
health=health-1
If health=0
FreeEntity shp\entity
Delete shp
EndIf
Else

;left and right turning-----------------------
If KeyDown( 203 )=True Then angle#=angle#+3.0
If KeyDown( 205 )=True Then angle#=angle#-3.0

;thrust---------------------------------------
If KeyDown( 200 )=True Then
shp\x#=shp\x#+Cos(angle#+90.0)*shp\dist#
shp\z#=shp\z#+Sin(angle#+90.0)*shp\dist#
EndIf

;brakes---------------------------------------
If Not KeyDown(200)
shp\x#=0+(shp\x#*0.995)
shp\z#=0+(shp\z#*0.995)
EndIf

;reposition on screen if off screen-----------
If EntityZ#(shp\entity)>100
PositionEntity shp\entity,shp\x#,shp\y#,-100
EndIf
If EntityZ#(shp\entity)>100
PositionEntity shp\entity,shp\x#,shp\y#,100
EndIf
If EntityX#(shp\entity)>120
PositionEntity shp\entity,-120,shp\y#,shp\z#
EndIf
If EntityX#(shp\entity)>120
PositionEntity shp\entity,120,shp\y#,shp\z#
EndIf

;movement of player----------------------------
RotateEntity shp\entity,0.0,angle#,0.0
TranslateEntity shp\entity,shp\x#/2,0.0,shp\z#/2
fire()
EndIf
Next

End Function

;Bullet functions---------------------------------
Function fire()
;fire new bullet
If KeyDown(157) ;change to MouseHit(1) for mouse button 1
timage=timage+1
If timage=1
If numshot<3
shoot.bullet = New bullet
shoot\entity = CopyEntity(bullet_entity,ship) ;<parent> parameter here is IMPORTANT
EntityParent shoot\entity, 0 ;remove from parent so it can move on its own
shoot\distance_to_travel = 70
numshot=numshot+1
EndIf
EndIf
ElseIf Not KeyDown(157)
timage=0
EndIf
If timage>10 Then
timage=0
EndIf
End Function

Function MoveBullets()
;-- go through all active bullets...
For shoot.bullet = Each bullet
zap=EntityCollided(shoot\entity,rock_collision)
If zap
FreeEntity shoot\entity
If numshot>0
numshot=numshot-1
EndIf
Delete shoot
Else
;move bullet forward
MoveEntity shoot\entity, 0,0,2
;reduce counter of distance to travel until finished movement
shoot\distance_to_travel = shoot\distance_to_travel - 1
;remove bullet entity? (finished it's movement (or collided with block))
If shoot\distance_to_travel=0
;remove entity and delete bullet type instance
FreeEntity shoot\entity
If numshot>0
numshot=numshot-1
EndIf
Delete shoot
Else
If EntityZ#(shoot\entity)>100
PositionEntity shoot\entity,shoot\x#,shoot\y#,-100
EndIf
If EntityX#(shoot\entity)>120
PositionEntity shoot\entity,-120,shoot\y#,shoot\z#
EndIf
If EntityZ#(shoot\entity)<-100
PositionEntity shoot\entity,shoot\x#,shoot\y#,100
EndIf
If EntityX#(shoot\entity)<-120
PositionEntity shoot\entity,120,shoot\y#,shoot\z#
EndIf
EndIf
EndIf
Next
End Function