How to move point along circle?
Solution 1
You can do it with magic of anchor point in a really simple way =) All the transformations with any node are done relatieve to it's anchor point. Rotation is a kind of transformations too. So, you can do something like this
CGFloat anchorY = neededRadius / spriteHeight;
[yourSprite setAnchorPoint:ccp(0.5f, anchorY)];
[yourSprite setPosition:neededPosition]; //remember that position is also for anchor point
[self addChild: yourSprite];
[yourSprite runAction:anyRotateAction]; // i mean, CCRotateBy or CCRotateTo
Solution 2
Try this:
a1 = 28*(π/180)
cen.x = p1.x - r*SIN(a1)
cen.y = p2.y - r*COS(a1)
a2 = a1 + l/r
p2.x = cen.x + r*SIN(a2)
p2.y = cen.y + r*COS(a2)
It does not get any simpler than this. Move from p1 to the circle center, and then to p2.
Solution 3
Calculate the origin, and then use polar co-ordinates to move the point along the circle.
Comments
-
Jonathan almost 2 years
I want to move a sprite in a circular motion by using a circle radius and movingangle.
I know for instance that the sprite is moving in a circle with the radius 10 and It's current position is (387,38) and angle is 28 degrees. Now I wann't move it say, 100px along the circle perimeter.
p1 = x&y coordinate, known (387,38) in example with movingangle 28 degrees r = radius, known (10 in example) l = distance to move along bend, known (100px in example) p2 = new point, what is this x and y value?
I have a solution that works but I dont quite like it. It kind of don't make sense to me and it requires more calculations than I think is required. It works by first calculating the center point using p1 and then doing the same thing backwards so to speak to get p2 (using cosinus and sinus). There should be a quicker way I believe but I cant find anyone doing exactly this.
The reason i tagged cocos2d is because thats what I'm working with and sometimes game frameworks provide functions to help with the trigonometry.
-
Jonathan almost 12 yearsI think I tried something similar, I'll try it and see how it goes. Thanks
-
andrew cooke almost 12 yearshow does this help? it assumes you're centred at the origin. if you're not, you need to work out where you are. which is calculating the centre. which is what the question specifically asks to avoid.
-
Jonathan almost 12 yearsThats true, it will move in a circular motion but not in the correct place.
-
John Alexiou almost 12 years@MrAzulay - The origin needs to be known, or calculated.
-
LearnCocos2D almost 12 yearsdon't change the anchorpoint, instead add the sprite to a regular CCNode and offset it from the CCNode via position so that the CCNode is the center of the circle. Then you can change rotation of the CCNode to rotate the sprite. It's easier and makes sure that for example sprite collisions are still straightforward.