here:
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.
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.