Collision's From a Ball to ???

Blitz3D Forums/Blitz3D Programming/Collision's From a Ball to ???

I'm working on some code just now, that requires, a Ball or Circle i should say, to collide off of a few different shapes.

Circle - Circle
Circle - Tri
Circle - Squ
Circle - Star
Circle - flatwall or line.

I am currently working on the following code.

Graphics3D 500,500,16,2
SetBuffer BackBuffer()
Cls

SeedRnd MilliSecs()

Type Sphere

	Field x#
	Field y#

	Field angle#
	
	Field velx#
	Field vely#
	
	Field size%
	Field id%
	
	Field hit
	
	Field velocity#
	
	Field Colltimer%
	
End Type

Global Ball.sphere = New sphere : Delete ball
Global ballcounter%

For looper=1 To 2
	create()
Next

Repeat 
Cls
	update()
	render()
Flip
Until KeyHit(1)

Function Create()
	ballcounter=ballcounter+1
	Ball.sphere = New sphere
		Ball\x=Rand(100,400)
		Ball\y=Rand(100,400)
		Ball\angle=Rand(-180,180)
		Ball\size = 30
		ball\velx = Sin(ball\angle)
		ball\vely = Cos(ball\angle)
		ball\id = ballcounter
		ball\hit=False
		ball\colltimer=0
		ball\velocity#=3
End Function

Function update()
	If KeyHit(57) Then create()
	
	
	For Ball.sphere = Each sphere
		If ball\colltimer>0 Then ball\colltimer=ball\colltimer-1
			
		ball\x=ball\x+ball\velx*ball\velocity
		ball\y=ball\y+ball\vely*ball\velocity

		If ball\x<0 Or ball\x>500-(ball\size) Then ball\velx=-ball\velx
		If ball\y<0 Or ball\y>500-(ball\size) Then ball\vely=-ball\vely
		
		
		
		;check for collisions.
		For ball2.sphere = Each sphere
		
			If ball2<>ball
				;do checks here
				CollisionDistance# = (ball\size+ball2\size)/2
				Local xdist#,ydist#,dist#
				xdist#=Abs(ball2\x-ball\x)
				ydist#=Abs(ball2\y-ball\y)
				distance = Sqr(xdist*xdist+ydist*ydist)
				DebugLog distance
				
				;Collided or not?
				If Distance < collisionDistance Then
					
					;Get the Angle from Ball 1 to Ball 2
					Normal_Angle#=ATan2(ball2\y-ball\y, ball2\x-ball\x)
	
					;Work out, How much they are overlapping.
					moveback1#=(collisionDistance-Distance)
					moveback2#=(collisionDistance-Distance)
					
					;Move Bals Back along the normal Angle by the amount they overlap.
					ball\x=ball\x + moveback1*Cos(Normal_Angle+180)
					ball\y=ball\y + moveback1*Sin(Normal_Angle+180)
					ball2\x=ball2\x + moveback2*Cos(Normal_Angle+180)
					ball2\y=ball2\y + moveback2*Sin(Normal_Angle+180)
					
					;we now need to bounce them away .. reflection.
					nX#=Cos(Normal_Angle)
					nY#=Sin(Normal_Angle)
					
					ball\velx = ball\velx - nX 
					ball\vely = ball\vely - ny
					ball2\velx = ball2\velx + nX 
					ball2\vely = ball2\vely + ny
			
				End If
			End If	
		
		Next
	Next
	

End Function

Function render()
	For Ball.sphere = Each sphere
		Text ball\x,ball\y-5,Str(ball\id),1,1
		If ball\hit=True
			Color 255,0,0
			Oval(Ball\x,Ball\y,Ball\size,Ball\size,1)
			ball\hit=False
		Else
			Color 0,255,0
			Oval(Ball\x,Ball\y,Ball\size,Ball\size,1)
		End If
	Next
End Function


Space Bar, will add another circle, at the moment, I seem to be getting a change in velocity(x,y) from some collisions and I dont want that, I need for the collision to only change the vector the circle is moving along and not its speed.

I also need as stated above to work out how to best detect a collision vs a square, and have the circle reflect at the correct angle, but im not sure where to start with that, not to mention a star lol..

Anyway . thanks in advance for any help.

fyi math aint my strong point, my brain works maths problems out differently to normal people so I appologise if its something you might consider stupid.

it aint a trivial problem at all

Couldn't you just use Blitz collisions with the stop method?

Then check the collision normals for the angles (CollisionNX, ...NZ, etc.), and then just reverse (* -1) the velocity of the axis' that hit?

Much better to use the inbuilt collisions for this ... especially non-circular shapes. It will save you some major headaches .. trust me. Check out the code archives ... I think Sswift and Darkeagle have some code there to calculate the correct angle of reflection. These will be for 3d ..

These examples will not assume the velocity magnitude is preserved on collision which is physically correct. I guess you could normalise the resulting reflection velocities and multiply them by the speed you want to retain. I don't think it would look right though.

the collisions aint to much of a problem, as i do in the end intend to use the built in blitz collisions.

Its more that I need help working out the reflection angles after a collision.