Someone elses code ... I forget who ?? It handles purely elastic collisions ..
Graphics3D 640,480,16,1
Const C_BALL = 1
Const C_CUBE = 2
Const BALLS = 50
Const SPEED# = .5
Global camera,cube
Global light =CreateLight()
Type ball
Field entity
Field xvel#,yvel#,zvel#
End Type
Collisions C_BALL, C_BALL,1,1
Collisions C_BALL, C_CUBE,2,1
MakeStuff()
While Not KeyDown(1)
UpdateBalls()
UpdateWorld()
RenderWorld()
Flip
Wend
;==============================================
;==============================================
;==============================================
Function MakeStuff()
;cube
cube = CreateCube()
ScaleMesh cube,30,30,30
FlipMesh cube
EntityColor cube,50,50,200
EntityType cube,C_CUBE
UpdateNormals cube
;camera
camera = CreateCamera()
PositionEntity camera,0,60,0
PointEntity camera, cube
;template for balls
temp = CreateSphere()
EntityType temp,C_BALL
UpdateNormals temp
EntityShininess temp,1
HideEntity temp
;balls
For l = 1 To BALLS
b.ball = New ball
b\xvel = Rnd(-SPEED,SPEED)
b\yvel = Rnd(-SPEED,SPEED)
b\zvel = Rnd(-SPEED,SPEED)
b\entity = CopyEntity( temp )
PositionEntity b\entity,Rnd(-30,30),Rnd(-30,30), Rnd(-30,30)
EntityColor b\entity,Rand(100,255),Rand(100,255),Rand(100,255)
radius# = Rnd( 1,4 )
ScaleEntity b\entity,radius,radius,radius
EntityRadius b\entity,radius
Next
End Function
;==============================================
;==============================================
;==============================================
Function UpdateBalls()
For b.ball = Each ball
For i = 1 To CountCollisions(b\entity)
HandleBounce(b,i)
Next
TranslateEntity b\entity,b\xvel,b\yvel,b\zvel
Next
End Function
;==============================================
;==============================================
;==============================================
Function HandleBounce(b.ball,i)
; Get the normal of the surface collided with.
Nx# = CollisionNX(b\entity, i)
Ny# = CollisionNY(b\entity, i)
Nz# = CollisionNZ(b\entity, i)
; Compute the dot product of the ball's motion vector and the normal of the surface collided with.
VdotN# = b\xvel*Nx + b\yvel*Ny + b\zvel*Nz
; Calculate the normal force.
NFx# = -2.0 * Nx# * VdotN
NFy# = -2.0 * Ny# * VdotN
NFz# = -2.0 * Nz# * VdotN
; Add the normal force to the motion vector.
b\xvel = b\xvel + NFx
b\yvel = b\yvel + NFy
b\zvel = b\zvel + NFz
End Function