Bounching ball

BlitzMax Forums/BlitzMax Programming/Bounching ball

Hello all!

As still a new beginner in BlitzMax, alot of questions need to be answered. So, here goes. :P

I have a problem with my code, I can't get the ball to go in the right directions when bounching of the rectangle. I have looked at several collision sollutions here in the forum, but. I can't figure out how to do this with my own code. Please help! :D

PS! I must admit that I ain't no mathematical cast. :)

Thanks in advance,
Docster


Graphics 800,600,0

Cls
' Create our ball
DrawOval 0,0,20,20
Global img_ball=CreateImage(21,21,1,DYNAMICIMAGE|MASKEDIMAGE)
GrabImage img_ball,0,0

Cls
' Create our rectangle
DrawRect 0,0,220,160
Global img_rect=CreateImage(220,160,1,DYNAMICIMAGE|MASKEDIMAGE)
GrabImage img_rect,0,0
AutoMidHandle True

Global spd:Float=6
Global x:Float
Global y:Float
Global angl:Float = 90;

SeedRnd MilliSecs()
x=Rand(10,100)
y=Rand(10,100)

Global cx:Float=GraphicsWidth()/2-ImageWidth(img_rect)/2
Global cy:Float=GraphicsHeight()/2-ImageHeight(img_rect)/2
Global angle:Float=45

Repeat
	Cls
	ResetCollisions
	
	x:+Cos(angle) * spd
	y:+Sin(angle) * spd

	DrawImage img_ball,x,y,0

	' Bounch walls
	If x<10 Or x>GraphicsWidth()-10
		angle=180-angle
	End If
	If y<10 Or y>GraphicsHeight()-10
		angle=-angle
	End If
	
	' Bounch rectangle
	If ImagesCollide2(img_rect,cx,cy,0,0,1,1,img_ball,x,y,0,0,1,1)
		angle=-angle + ang(x,y,cx,cy)
	End If

	DrawImage img_rect,cx,cy,0

	Flip 1

Until MouseHit(1)

Function Ang:Float(x1:Float,y1:Float,x2:Float,y2:Float)
	Local dx:Float=x1-x2
	Local dy:Float=y1-y2
	Return 180.0-ATan2(dy,dx)
End Function


To start with, the parameters of Atan2() should be swapped :

ATan2(dy, dx)

Didn't have any much effect on the rest though. :P

I would say that approach is doomed.

Notice that you have different responses for vertical and horizontal walls.
But the collision response with the rectangle is the same in all cases, i.e. the geometry is completely ignored.

Is there any way to find out what side of the recangle the ball hits?

here:
Graphics 800,600,0

Cls
' Create our ball
DrawOval 0,0,20,20
Global img_ball=CreateImage(21,21,1,DYNAMICIMAGE|MASKEDIMAGE)
GrabImage img_ball,0,0

Cls
' Create our rectangle
DrawRect 0,0,220,160
Global img_rect=CreateImage(220,160,1,DYNAMICIMAGE|MASKEDIMAGE)
GrabImage img_rect,0,0
AutoMidHandle True

Global spd:Float=6
Global x:Float
Global y:Float
Global angl:Float = 90;

SeedRnd MilliSecs()
x=Rand(10,100)
y=Rand(10,100)

Global cx:Float=GraphicsWidth()/2-ImageWidth(img_rect)/2
Global cy:Float=GraphicsHeight()/2-ImageHeight(img_rect)/2
Global angle:Float=45

Local dx:Float = Cos(angle) * spd
Local dy:Float = Sin(angle) * spd

Repeat
	Cls
	ResetCollisions
	DrawImage img_ball,x,y,0
	' Bounch walls
	If (x+dx)<10 Or x+dx>GraphicsWidth()-10 Then dx = - dx 
	If (y+dy)<10 Or y+dy>GraphicsHeight()-10 Then dy = - dy
	If ImagesCollide2(img_rect,cx,cy,0,0,1,1,img_ball,x+dx,y,0,0,1,1) dx = - dx
	If ImagesCollide2(img_rect,cx,cy,0,0,1,1,img_ball,x,y+dy,0,0,1,1) dy = - dy
	x:+ dx
	y:+ dy
	DrawImage img_rect,cx,cy,0
	Flip 1

Until MouseHit(1)

Function Ang:Float(x1:Float,y1:Float,x2:Float,y2:Float)
	Local dx:Float=x1-x2
	Local dy:Float=y1-y2
	Return ATan2(dy,dx)
End Function



this approach only works with horizontal and vertical contacts. If you are going to do different angle contact, you are going to have to find a different approach. Your best option is to use vectors but This should suffice for your sample code.

Thanks for all help. I rewrote the entire code, and found a "good" solution for my situation. :)