Circle Collision by HeavyAmp26th Mar 2007 16:01
|
---|
Summary This Function Detects if two circles have collided and will then apply the collision to the circles. This collision doesn't rely on forces to repel each circle making it more accur Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com Sync On : Set display mode 1280,1024,32 Type Unit x as float y as float radius as float EndType `Create Units Dim Unit(2) as Unit `Initialize Units Unit(1).Radius=50 Unit(1).x=100 Unit(1).y=100 Unit(2).x=(screen width()/2) Unit(2).y=(screen height()/2) Unit(2).Radius=100 Do Set Cursor 0,0 : Print "Use Arrow keys to move object" `Move Object If Upkey()=1 then Unit(1).Y=Unit(1).Y-1 If Downkey()=1 then Unit(1).Y=Unit(1).Y+1 If Leftkey()=1 then Unit(1).X=Unit(1).X-1 If Rightkey()=1 then Unit(1).X=Unit(1).X+1 `Implement Collsion Collision(Unit(1).x, Unit(1).y, Unit(1).Radius, Unit(2).x,Unit(2).y,Unit(2).Radius) Unit(1).x = ReturnValueX Unit(1).y = ReturnValueY `Draw Objects Circle Unit(1).x,Unit(1).y,Unit(1).Radius Circle Unit(2).x,Unit(2).y,Unit(2).Radius sync : cls loop Function Collision(Object1X as Float, Object1Y as Float , Object1Radius as Float, Object2X as Float, Object2Y as Float, Object2Radius as Float) Local DistX as Float : Local RadCord1X as Float : Local RadCord2X as Float : Local MoveX as Float Local DistY as Float : Local RadCord1Y as Float : Local RadCord2Y as Float : Local MoveY as Float Local Distance as Float : ColAngle as Float `Values that we send Back Global ReturnValueX as Float Global ReturnValueY as Float `Set the return Value to equal the Objects Position ReturnValueX = Object1X ReturnValueY = Object1Y `Calculate the distance between the two units Distance = sqrt((Object1X - Object2X)^2 + (Object1Y - Object2Y)^2) `If the distance is less than their radius, ie If they have collided If Distance < Object1Radius + Object2Radius `Calculate the X and Y distance between Each Unit DistX=(Object2X)-(Object1X) DistY=(Object2Y)-(Object1Y) `Calculate the Angle between the Two Units `Note: Use atan2() in c++ ColAngle=ATANFULL(DistY,DistX) `Get the cords of the First Units radius in the direction of the collsion DistX=Cos(ColAngle)*(Object1Radius) DistY=Sin(ColAngle)*(Object1Radius) `Add the distance to the first units cords RadCord1X=(Object1X + DistX) RadCord1Y=(Object1Y + DistY) `Get the cords of the Second Units radius in the direction of the collsion DistX=Cos(ColAngle)*(Object2Radius) DistY=Sin(ColAngle)*(Object2Radius) `Minus the distance to the second units cords RadCord2X=(Object2X - DistX) RadCord2Y=(Object2Y - DistY) `Get the distance between the two radius cords DistX=(RadCord1X)-(RadCord2X) DistY=(RadCord1Y)-(RadCord2Y) `Get the Distance to move the Unit away From the Other Unit MoveX=cos(ColAngle)*(DistX) MoveY=sin(ColAngle)*(DistY) `Set the return value to equal our new objects position If DistX<0 then ReturnValueX = Object1X + MoveX If DistY<0 then ReturnValueY = Object1Y + MoveY If DistX>0 then ReturnValueX = Object1X - MoveX If DistY>0 then ReturnValueY = Object1Y - MoveY EndIf EndFunction `(ReturnValueX,ReturnValueY) |