ball hits a pointy bit, what happens?

Miscellaneous Forums/General Discussion/ball hits a pointy bit, what happens?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I'm not certain if you will be able to use this, but, it may help.

Const	ORB_MAX = 4

;Collision
Const	COLLISIONMETHOD_ELLIPSOID	=	1
Const	COLLISIONMETHOD_POLYGON	=	2
Const	COLLISIONMETHOD_BOX	=	3
Const	COLLISIONRESPONSE_STOP	=	1
Const	COLLISIONRESPONSE_SLIDE	=	2
Const	COLLISIONRESPONSE_SLIDENOSLOPE	=	3

Const	COLLISIONTYPE_NONE	=	0
Const	COLLISIONTYPE_ROOM	=	1
Const	COLLISIONTYPE_ORB	=	2

Type Orb
	;Purpose: Orb object
	;Properties:
	Field ID
	Field entity%;
	Field velx#; velocity vector used in Phsysics
	Field vely#;
	Field velz#;
	Field radius#;
End Type

Global Orb_ID.Orb[ORB_MAX]
Global Orbs

Function  OrbNew.Orb()
	;Purpose: Creates HP_Orb Instance; Sets Default Property Values
	;Parameters: TBD
	;Return: HP_Orb Instance		
	this.Orb = New Orb
	Orbs = Orbs + 1
	this\ID = Orbs
	Orb_ID[this\ID] = this
	this\entity% = CreateSphere(32)
	EntityShininess(this\entity%,.7)
	EntityColor(this\entity,255,0,0)
	NameEntity(this\entity%,this\ID)
	this\radius = .5
	ScaleEntity(this\entity%,this\radius#,this\radius#,this\radius#)
	EntityRadius(this\entity%,this\radius#)
	EntityType(this\entity%,COLLISIONTYPE_ORB)
	Return this 
End Function

;create a big cube for ROOMs here 

Function OrbUpdate()
	;Purpose: Main Loop Update. Updates all HP_Orb Instances.
	;Parameters: None
	;Return: None
	For this.Orb = Each Orb
		
		;Orb Physics, Special Thanks to Stevie G & DarkEagle 
		
		;gradually slow down
		;this\velx=this\velx*0.99
		;this\vely=this\vely*0.99
		;this\velz=this\velz*0.99		
		
		For OrbCountCollisions = 1 To CountCollisions(this\entity)
			; Calculate Bounce	
			
			;Get position of the surface collided with.
			OrbPhysicsX# = CollisionX#(this\entity, OrbCountCollisions) 
			OrbPhysicsY# = CollisionY#(this\entity, OrbCountCollisions) 
			OrbPhysicsZ# = CollisionZ#(this\entity, OrbCountCollisions) 
			
			; Get the normal of the surface collided with. 
			OrbPhysicsNx# = CollisionNX#(this\entity, OrbCountCollisions) 
			OrbPhysicsNy# = CollisionNY#(this\entity, OrbCountCollisions) 
			OrbPhysicsNz# = CollisionNZ#(this\entity, OrbCountCollisions) 
			
			;move it slightly away from collision
			TranslateEntity(this\entity, .01 * OrbPhysicsNx , .01 * OrbPhysicsNy , .01 * OrbPhysicsNz)
							
			; Compute the dot product of the ball's motion vector and the normal of the surface collided with. 
			OrbPhysicsVdotN# = this\velx#*OrbPhysicsNx# + this\vely#*OrbPhysicsNy# + this\velz#*OrbPhysicsNz# 
	
			;set coeficient of restitution
			OrbPhysicsCOR# = .987654321 	
			;OrbPhysicsCOR# = 0.0	;inelastic collision
			;OrbPhysicsCOR# = 1.0 ;elastic collision
			
			; Calculate the normal force. 
			OrbPhysicsNFx# = -( 1.0 + OrbPhysicsCOR ) * OrbPhysicsNx# * OrbPhysicsVdotN# 
			OrbPhysicsNFy# = -( 1.0 + OrbPhysicsCOR ) * OrbPhysicsNy# * OrbPhysicsVdotN# 
			OrbPhysicsNFz# = -( 1.0 + OrbPhysicsCOR ) * OrbPhysicsNz# * OrbPhysicsVdotN# 
			
			this\velx# = this\velx# + OrbPhysicsNFx#
			this\vely# = this\vely# + OrbPhysicsNFy#
			this\velz# = this\velz# + OrbPhysicsNFz#
					
			;Get Entity Type of the surface collided with. 	
			OrbCollisionEntity = CollisionEntity(this\entity,OrbCountCollisions)
			Select GetEntityType (OrbCollisionEntity)
			
				Case COLLISIONTYPE_ROOM
					;do stuff to Orb or ROOM entity here
										
				Case COLLISIONTYPE_ORB
					Orb2.Orb=Orb_ID[EntityName(OrbCollisionEntity)]
					;inverts the velocity vector						
					Orb2\velx=-this\velx
					Orb2\vely=-this\vely
					Orb2\velz=-this\velz
						
			End Select			
			
		Next

		TranslateEntity(this\entity, this\velx, this\vely, this\velz)

	Next
End Function	

;==============
;MAIN
;==============

.MAIN_START
	;setup 3D before calling the next commands
	Graphics3D(800,600,32,2)
	SetBuffer(BackBuffer())
	
	Collisions(COLLISIONTYPE_ORB,COLLISIONTYPE_ORB,COLLISIONMETHOD_ELLIPSOID,COLLISIONRESPONSE_SLIDE)
	Collisions(COLLISIONTYPE_ORB,COLLISIONTYPE_ROOM,COLLISIONMETHOD_POLYGON,COLLISIONRESPONSE_SLIDE)
	
	;setup camera and lights here
	Camera=CreateCamera()
	PositionEntity(Camera,0,0,-20) 
	CameraLight=CreateLight()
	RotateEntity(CameraLight,90,0,0)
	
	;setup big cube as a room
	Room = CreateCube()
	ScaleEntity(Room,10,10,10)
	FlipMesh(Room)
	EntityType(Room,COLLISIONTYPE_ROOM)
	
	;setup some orbs	
	Dim Orb.Orb(ORB_MAX)
	
	For loop = 1 To ORB_MAX
		;create some Orbs using the OrbNew() function
		Orb(loop) = OrbNew()
	
		;set Rnd velocity vector for Orbs 
		Orb(loop)\velx = Rnd(-1,1)
		Orb(loop)\vely = Rnd(-1,1)
		Orb(loop)\velz = Rnd(-1,1)
	Next


.MAIN_UPDATE
	Repeat
	
		OrbUpdate()
		
		UpdateWorld() 
		RenderWorld()
		
		Flip()
		
	Until KeyHit(1)


.MAIN_STOP

	End