Breakout physics using Collision Normals

Blitz3D Forums/Blitz3D Programming/Breakout physics using Collision Normals

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 hate the game :) However the ball bounce stuff was all worked out here some time ago by the likes of SSwift. Still on the forums if you search for it. I have changed your code to include it, and tried adding paddle forces, but that bit needs improving.

Graphics3D 640,480,16,1
HidePointer()

camera=CreateCamera()
light=CreateLight()
LightColor light,100,100,100
RotateEntity light,180,0,0

Const CUBE_COL=1
Const SPHERE_COL=2

;create ball
Global ball=CreateSphere(12)
ScaleEntity ball,3,3,3
EntityColor ball,100,100,255
EntityShininess ball,.5
maxballspd#=5

;create paddle
Global p1=CreateCube()
ScaleEntity p1,20,3,2 
EntityColor p1,200,200,0

;setup Collisions table
EntityType ball,SPHERE_COL
EntityType p1, CUBE_COL
Collisions SPHERE_COL,CUBE_COL,2,2

arena=CreateCube()
EntityColor arena,200,100,0
FlipMesh arena : ScaleEntity arena,200,150,5
EntityType arena,CUBE_COL

Local vx#=2,vy#=2

While Not KeyHit(1)

	x1#=x1#+MouseXSpeed()*0.5
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	oldx#=EntityX(p1)	
	If x1#<-180 Then x1#=-180
	If x1#>180 Then x1#=180
	PositionEntity p1,x1#,122,0
	PFx#=EntityX(p1)-oldx
	PositionEntity camera,0,0,200
	RotateEntity camera,180,0,0

	If EntityCollided(ball,CUBE_COL) Then
		Nx# = CollisionNX(ball, 1)
		Ny# = CollisionNY(ball, 1) 
		; Compute the dot product of the entity's motion vector and the normal of the surface collided with. 
		VdotN# = Vx#  * Nx# + Vy# * Ny# 
		; Calculate the normal force. 
		NFx# = -2.0 * Nx# * VdotN#
		NFy# = -2.0 * Ny# * VdotN#
		; Add the normal force to the direction vector.
		; if coll with paddle include some paddle force
		If CollisionEntity(ball,1)=p1 Then 
			Vx# = Vx# + NFx# + PFx#*0.5 
			Vy# = Vy# + NFy# - Abs(PFx#*0.1)
		Else
			Vx# = Vx# + NFx#
			Vy# = Vy# + NFy#
		EndIf

	EndIf
	
	;limit ballspeed
	ballspd# = Sqr(Vx# * Vx# + Vy# * Vy#)

	If ballspd# > maxballspd# Then
		Vx# = Vx# / ballspd# * maxballspd#
		Vy# = Vy# / ballspd# * maxballspd#
	EndIf	

	TranslateEntity  ball,Vx,Vy,0
	PositionEntity ball,EntityX(ball),EntityY(ball),0  ; stay at the same Z
	
	UpdateWorld
	RenderWorld
	
	Flip

Wend
End