cocos2d help find points on a circle - iphone

I am trying to solve a tricky math problem, in a cocos2d for iphone context.
Basically I have a roullette wheel which is rotating over time.
I want to have a Sprite latch onto the wheel at certain points (like compass ordinal points N, S, E, W) and bounce off at all other points.
I have the image of the wheel rotating, and just need to solve the part where I can test for whether a sprite has intersected with the circle at the right point on the circle as it is rotating.
I think this question is going in the right direction, but I can't get my head around it. Can anyone help explain?
Best way to find a point on a circle closest to a given point

If I understand correctly:
First check the distance between the sprite and the centre of the roulette wheel. This will tell you if the sprite is at the edge of the wheel. (If not, nothing happens, right?)
Then, find the angle that the sprite makes from the "x-axis" of the roulette wheel.
spriteAngle = atan2(sprite.x - rouletteCentre.x, sprite.y - rouletteCentre.y)
You'll need to find the equivalent of the atan2() function. It usually returns an answer in radians; you may want to convert it to degrees or quarter-turns or something if you prefer.
Then, subtract the angle that the roulette wheel itself is rotated by (if the wheel itself is rotating, if not then you're already done). Make sure your angle measurement is consistent.
actualAngle = spriteAngle - rouletteRotationAngle
Note that actualAngle may be outside the range 0-360 degrees, and you will need to make it "wrap around".
Lastly, you will want to allow a small range of values as acceptable (e.g. 98 degrees to 102 might count as "North").

So you have a circle of radius r, with center (x0,y0).
A point lies outside of the circle, at coordinates (x,y). Your question is to find the closest point on the circle itself to the point (x,y).
The solution is simple. The closest projection of a point onto a circle is accomplished by a simple scaling. Thus,
d = sqrt((x-x0)^2 + (y-y0)^2)
xp = x0 + (x - x0)*r/d
yp = y0 + (y - y0)*r/d
The new point (xp,yp) will lie on the circle itself. To be honest, you would be better off to work in polar coordinates, with the origin at the center of the circle. Then everything gets much easier.
Your next question will be where did it hit on the circle? Don't forget the points of the compass on the circle are rotating with time. An atan2 function will give you the angle that the point (xp-x0,yp-y0) lies at. Most toolsets will have that functionality. See that I've subtracted off the origin here.

Related

Is it possible to use OpenPose output of x and y coordinates to detect rotating movement?

I did some research on OpenPose and the output is x and y coordinates with confidence point. x and y coordinates are good for detecting up, down, left, and right movements. I was wondering is it possible to detect turning movements. Turning usually happens on the z axis. Is there a way to tell if body part has rotated 180 degrees with x and y coordinate.
I had few ideas like calculating the slope of the hand line. The slope tells us if the hand is tilted or not. If the slope is high or very low then the hand has rotated. Same concept for all other body parts. But I don't think that will work in all cases. Please check 2nd figure here https://cmu-perceptual-computing-lab.github.io/openpose/web/html/doc/md_doc_02_output.html to understand output of OpenPose.

Unity Rotate Sphere To Point Directly Upwards Based On Child Point

I've got a 3d sphere which I've been able to plot a point on using longitude and latitude thanks to some work of another developer I've found online. I think I understand what its doing.
What I need to do now is rotate my planet so the point is always at the top most point (ie the north pole) but I'm not sure how to do this. I'm probably missing some important fundamentals here so I'm hoping the answer can assist in my future learning.
Here's an image showing what I have - The blue line is a line coming from the longitude and latitude I have plotted and I need to rotate the planet so that line is basically pointing directly upwards.
https://ibb.co/2y24FxS
If anyone is able to advise it'd be very much appreciated.
If I'm not mistaken, Unity uses a coordinate system where the y-axis points up.
If the point on your sphere was in the xy-plane, you'd just have to determine the angle between the radius-vector (starts in the center of the sphere, ends on the point in question) and the y-axis, and than rotate by that amount around the z-axis, so that the radius vector becomes vertical. But your point is at an arbitrary location in 3D space - see the image below. So one way to go about it is to first bring the point to the xy-plane, then continue from there.
Calculate the radius vector, which is just r = x-sphereCenter. Make a copy of it, set y to zero, so that you have (x, 0, z) - which is just the projection of the vector r on the horizontal xz-plane - let's call the copy rXZ.
Determine the signed angle between the x-axis and rXZ (use Vector3.SignedAngle(xAxis, rXZ, yAxis), see docs), and create a rotation matrix M1 that rotates the sphere in the opposite direction around the vertical (negate the angle). This should place your point in the xy-plane.
Now determine the angle between r and the y-axis (Vector3.SignedAngle(r, yAxis, zAxis)), and create a new rotation matrix M2 that rotates by that angle around the zAxis. (I think for this second one, the simpler Vector3.Angle will work as well.)
So, what you want now is to combine the two matrices (by multiplying them) into a single transform (I'm assuming this is a transformation in the local coordinate system of the sphere, where (0, 0, 0) is the sphere's center). If I'm not mistaken, Unity uses column-major matrices, so the multiplication order should be M = M2 * M1 (the rightmost matrix is applied first).
Reorient your globe using M as a local transform, and it should bring your point to the top. You can also create M3 = M1.inverse, and then do M = M3 * M2 * M1, to preserve the original angular offset from the xy-plane.
Check for edge cases, such as r already being vertical (pointing straight up, or straight down).

Two rotations at the same time, on the same axis

I need help regarding two rotations at the same time, on the same axis. One vector is approaching 90 degrees. When I press up, it should smoothly approach the angle 45 degrees above it, such that HOLDING up will still be going down. Here is a rough sketch of what I mean: (black = vector without up, red = vector approaching 45 above black)
Please refer the official documentation on
Quaternion.Lerp here
Quaternion.Slerp here
Choose whichever suits your needs.

When rotating an image about its center, why do you need to add/subtract the centroid?

I'm learning about image processing in class, and it's all going quite fast so I'm having a bit of trouble wrapping my head around the different concepts. In rotation specifically, I understand that you need to multiply each of the pixel coordinates of an input image by the rotation matrix to get an output image. However, when you are rotating an image around its center, you need to do a translation to subtract the centroid, then rotate, then add the centroid back in. What is the logic behind this, how does it work?
In general, when you rotate an object, you rotate it about origin (0,0). But if you want to rotate the object about some other point, which in your case is centroid, you move the origin to that point by subtracting it. After rotation, when you want the output in the same coordinate system as you began with, you bring back the origin to its normal position by adding it back.
The images below demonstrate it to make better sense-
The object to rotate initially.
When you rotate it by 135 degrees anticlock. Output1
But if you want to rotate about centroid, move the origin there.
How it now looks -
Rotating the same 135 degrees anticlock now-
Getting back to the old coordinate system by adding back the centroid - Output2
Clearly the Output1 and Output2 are not same right, so subtracting and adding the centroid or not depends on about which point you want to rotate.

device motion in constrained environment

I am trying to solve a seemingly easy problem related to device motion but couldn't figure out how to solve it. I have a situation where iPhone will move in a circle in the x-y plane. I need to find the angle between the iPhone's x and y axes relative to the center of rotation. The iPhone may be in portrait mode or landscape mode or in any angle in between relative to the line connecting iphone to the center of rotation. See the attached picture that explains the scenario.
The yaw change for a given rotation is the same regardless of this angle, so that doesn't really help. I am hoping that there would be some relationship that I can calculate for every small rotation and then find the best fit for the entire motion - but can't figure out that yet.
I appreciate any help or pointers.
(I am writing in pseudo-code since I don't know the API you are using, sorry.)
Here is how to get the axis and angle of the rotation.
Get the rotation matrices R1 and R2 at the beginning and end of your rotation directly from the API (see CMAttitude and CMRotationMatrix). Then, determine the angle and axis of the rotation R that brings R1 to align with R2. You get R as follows:
R = R1 * transpose(R2)
The angle of rotation R is
angle = acos((trace(R)-1)/2)
and its axis is
axis = { R[3][2]-R[2][3], R[1][3]-R[3][1], R[2][1]-R[1][2] }
For further details, please check Rotation matrix to axis angle and also Axis-angle.
I am not sure how to get the angle you are interested in. Nevertheless, I hope that the above helps.
Please don't use roll, pitch and yaw anything other than display. And don't try to integrate them, nothing good will come out.
Anyways, behind the rotation matrices there is integration. In other words, somebody already did the integration for you.