multiple copies bouncing off each other?

Blitz3D Forums/Blitz3D Programming/multiple copies bouncing off each other?

Lorenzo(Posted 2003) [#1]
does anyone have some code in which I can get multiple instances of an asteroid to bounce off each other? This is my code so far:

Graphics3D 640,480,16,1
SetBuffer BackBuffer()
SeedRnd (MilliSecs())
camera=CreateCamera ()
mainlight=CreateLight()
LightColor mainlight,255,255,10
sublight=CreateLight()
LightColor sublight,-255,-255,-255
indlight=CreateLight()
LightColor indlight,255,0,255

point=CreatePivot()
PositionEntity camera,0,100,0
PositionEntity mainlight,0,0,100
PositionEntity sublight,0,5,-0
PositionEntity indlight,0,0,-100

PositionEntity point,0,0,0
PointEntity camera,point
PointEntity mainlight,point
PointEntity sublight,point
PointEntity indlight,point

CameraViewport camera,0,0,viewx,viewy
Const viewx=800,viewy=600

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

;rockskin=LoadTexture("images/rocktext.bmp")
rock=LoadMesh("images/rock1a.3ds")
HideEntity rock

For n=0 To 9
ast.asteroid=New asteroid
ast\aster=CopyEntity(rock)
ast\x#=Rnd(-50,50)
ast\y#=0
ast\z#=Rnd(-50,50)
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#,ast\y#,ast\z#
RotateEntity ast\aster,0,Rnd(0,359),0
ScaleEntity ast\aster,.5,.5,.5
mark=mark+1
Next

;Main Loop-----------------
While Not KeyHit(1)
;Cls

asteroidism()


UpdateWorld
RenderWorld

Flip
Wend
End

Function asteroidism()
For ast.asteroid=Each asteroid

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

Next

End Function

I can't seem to get it. Any help is of course much appreciated.


Neo Genesis10(Posted 2003) [#2]
Here's a crude, but effective way of performing bounce checks.
Graphics3D 640,480
SetBuffer BackBuffer()

Collisions 1, 1, 1, 1

SeedRnd (MilliSecs()) 
camera=CreateCamera () 
mainlight=CreateLight() 
LightColor mainlight,255,255,10 
sublight=CreateLight() 
LightColor sublight,-255,-255,-255 
indlight=CreateLight() 
LightColor indlight,255,0,255 

point=CreatePivot() 
PositionEntity camera,0,50,0 
PositionEntity mainlight,0,0,100 
PositionEntity sublight,0,5,-0 
PositionEntity indlight,0,0,-100 

PositionEntity point,0,0,0 
PointEntity camera,point 
PointEntity mainlight,point 
PointEntity sublight,point 
PointEntity indlight,point 

CameraViewport camera,0,0,viewx,viewy 
Const viewx=800,viewy=600 

Type asteroid 
Field PIVOT			; ADDED new field - pivot
Field aster 
Field x# 
Field y# 
Field z# 
Field speedx# 
Field speedz# 
Field spin# 
Field life 
Field distance# 
End Type 

;rockskin=LoadTexture("images/rocktext.bmp") 
rock=LoadMesh("images/rock1a.3ds") 

EntityType rock, 1
 
HideEntity rock 

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

;Main Loop----------------- 
While Not KeyHit(1) 
;Cls 

asteroidism
UpdateWorld

CheckCollisions		; COLLISION CHECKS

RenderWorld 

Flip 
Wend 
End 

Function asteroidism() 
	For ast.asteroid=Each asteroid 
	
		TurnEntity ast\aster,ast\spin#,ast\spin#,ast\spin#
		MoveEntity ast\PIVOT, 0, 0, ast\speedz#	
		If EntityZ#(ast\aster)>100 
			PositionEntity ast\PIVOT,ast\x#,ast\y#,-100 
		EndIf 
		If EntityX#(ast\aster)>120 
			PositionEntity ast\PIVOT,-120,ast\y#,ast\z# 
		EndIf 
		If EntityZ#(ast\aster)<-100 
			PositionEntity ast\PIVOT,ast\x#,ast\y#,100 
		EndIf 
		If EntityX#(ast\aster)<-120 
			PositionEntity ast\PIVOT,120,ast\y#,ast\z# 
		EndIf
	
	Next 
End Function 

Function CheckCollisions()

	For ast.asteroid = Each asteroid
		For I = 1 To CountCollisions(ast\aster)
			coll_ent = CollisionEntity(ast\aster, I)
			a2.asteroid = GetAsteroid(coll_ent)
			
			; DO NOTHING IF ENTITY WASNT AN ASTEROID
			If a2 <> Null
				yaw1# = EntityYaw#(ast\PIVOT,1)
				yaw2# = EntityYaw#(a2\PIVOT,1)
				
				; A VERY crude bounce mechanism. You'll probably want
				; to code a better system.
				
				If yaw1# <= 90
					yaw1# = yaw1# + 90
				Else
					yaw1# = yaw1# - 90
				EndIf
				
				If yaw2# <= 90
					yaw2# = yaw2# + 90
				Else
					yaw2# = yaw2# - 90
				EndIf
				
				RotateEntity ast\PIVOT, EntityPitch#(ast\PIVOT,1), Yaw1#, EntityRoll#(ast\PIVOT,1)
				RotateEntity a2\PIVOT, EntityPitch#(a2\PIVOT,1), Yaw2#, EntityRoll#(a2\PIVOT,1)
			EndIf
			
		Next
	Next

End Function

Function GetAsteroid.asteroid( mesh )

	For ast.asteroid = Each asteroid
		If ast\aster = mesh Then Return ast
	Next
	Return Null

End Function
Notice first off, that instead of moving the asteroid itself I am moving the pivot it is parented to. I have also replaced TranslateEntity with MoveEntity. The reason for this is to allow the rotation of the pivot itself to dictate the direction of the asteroid. This way, by altering a single value (the pivots Yaw) you can change the direction of the asteroid.


Lorenzo(Posted 2003) [#3]
This code doesn't seem to work. It actually seems to lock up my computer. Thanks anyway.


Zethrax(Posted 2003) [#4]
Here's a couple of links that might be useful to you:

http://www.blitzbasic.com/codearcs/codearcs.php?code=751
http://www.blitzbasic.com/Community/posts.php?topic=27098


Neo Genesis10(Posted 2003) [#5]
Are you using it as-is or modifying it to fit your code? I have tested the above code and it works fine on my system.