Animating object on a circle
BlitzPlus Forums/BlitzPlus Beginners Area/Animating object on a circle
| ||
Imagine you have a circle, which would vary in size ... how do you animate a sprite on the edge of a circle, even if the circle changes size realtime?? |
| ||
Graphics 640,480,0,2 SetBuffer BackBuffer() radius# = 10 angle# = 0 Repeat angle = angle + 1.0 radius = 50 + Sin(angle*5)*40 Cls x# = 320+(Sin(angle)*radius) y# = 240+(Cos(angle)*radius) Rect x-3,y-3,7,7,True Oval 320-radius,240-radius,radius*2,radius*2,False Flip Until KeyHit(1) |
| ||
The formula for converting polar coordinates to cartesian is: X = Radius * Cos(Angle) Y = Radius * Sin(Angle) So, if I want to place a circle centered at 320, 240, and place a sprite at angle 0, which is the right side of the circle, then I would go: SpriteAngle# = 0 SpriteXOff# = CircleRadius# * Cos(SpriteAngle#) SpriteYOff# = CircleRadius# * Sin(SpriteAngle#) SpriteX# = 320.0 + SpriteXOff# SpriteY# = 240.0 + SpriteYOff# Note that 90 is the top of the circle, and that SpriteXOff# and SpriteYOff# will be negative at times, because the polar coordinate equation produces a circle centered at 0,0 in cartesian space. |