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
|