I want my Player sprite to rotate following the position of the mouse cursor.
I'm using atan2 to set up a 360 degree rotation action script for my Player sprite.
atan2(XMouse-XPlayer,YMouse-YPlayer) - 90
Using this, 90 degrees is straight up, 0 is direct right, 270 degrees is straight down. I need the Player sprite to sit on the Left side of the screen and only face the Right side of the screen when it rotates (meaning it can't look backwards).
I need to limit my angles to both conditions; less than 90 degrees OR greater than 270 degrees.
Ideally, I'd like to set 0 degrees to straight up, then I only have to set my conditions to Angles less than 180 degrees (one conditional rather than two).
Can that be done?
Yes. Use min() and max() (although you haven't said what language you're working in...)
If you use radians, then the condition to check for is that abs(theta) < PI/2 is always true. Here's an image from Wikipedia, just to show you what I mean.
In order to have 0 degrees straight up and clockwise movement from 0 to 360 degrees
use the formula:
f(x,y)=180-90*(1+sign(y))* (1-sign(x^2))-45*(2+sign(y))*sign(x)
-180/pi()*sign(x*y)*atan((abs(y)-abs(x))/(abs(y)+abs(x)))
Related
I have a beer bottle positioned at top of a glass at 90%. I want to rotate it around its Pivot which is at the top. In order to do so i'm trying to find the angle between the the mouseposition(mp) and the bottle and rotate ti by it.
The center of rotation is the current position of the GameObject since the Pivot of the sprite is at the top. I tried to find two Vectors one being the vector from mp to center of rotation and the other one being the position of the bottle. Then i used: gameObject.transform.Rotate(Vector3.Forward, Vector3.Angle(v2,v1)).
The result its not what i expected of course. I'm new to this game math, i'd appreciate an explanation.
(Its an android game and i intend to drag the bottle up and down from 90 to 180 degrees).
I hope that i understand you question correctly.
But if you want to find the angle between the mouse point you can use the two lines you have drawn on the picture and just calculate the angle between them.
Check out this answer:
Calculating the angle between two lines without having to calculate the slope? (Java)
I have calculated an axis angle rotations for each axis. If I only apply one of the three rotations, the objects rotates as expected.
If I multiply the rotation matrixes, as you would normally do it if you combine rotations I don't get the desired result, cause the first rotation affects the others and therefore the end result is not what I'm looking for.
I want to apply each rotation to the object as if it wasn't rotated before.
I guess this is a simple task, but it seems that I'm not searching for the right keywords. (Also the title is not perfect ... (open for suggestions))
Thanks for every hint / help.
You're experiencing gimbal lock. It's not a maths problem, it's a logical fallacy. Try it with, for the sake of description, your phone:
put it on your desk face up, with the charging connector at the bottom.
rotate clockwise around z by 90 degrees. So the charging connector is now to the left.
rotate around x (which will be the longer axis of the phone assuming the charging port is on the top or bottom) by 180 degrees.
the phone is now upside down, with the charging port on the left.
But:
put it on your desk face up, with the charging connector at the bottom.
rotate around x by 180 degrees. So the charging connector is now at the top.
rotate clockwise around z by 90 degrees.
the phone is now upside down, with the charging port on the right.
Which of those is incorrect if the instruction was to rotate 180 degrees around x and 90 degrees clockwise around z? Neither is incorrect. The instructions were ambiguous.
The order of individual rotations always matters, because the second affects the work done in the first, the third affects the work done in the first and second, etc. Shuffling the order just changes which affects which.
Orientation is normally stored directly as a matrix or as a quaternion because it's unambiguous and because it keeps the order of actions properly ordered.
Just stumbled into this question searching for something slightly different. Long story short, you are likely multiplying your matrices in reversed order.
Chaining rotations is a little counter-intuitive. If you have a chain of rotations R1*R2*R3*R4 then you have two ways to conceptualize what is happening: intrinsic and extrinsic.
The intrinsic view works from right to left. You start with R4 and work your way to R1. R4 takes the initial frame and produces a new frame that is rotated around R4's rotation axis. R4's axis is specified in the initial frame. R3 takes this rotated frame and rotates it around R3's rotation axis. Here, R3's axis is specified in R4's rotated frame. This continues down the chain, each time changing the frame in which the rotation axis is expressed.
The extrinsic view works from left to right. You start with R1 and work your way to R4. The output frame of R1 is rotated around R1's axis. R1's axis is expressed in the initial frame. R2 then takes both, R1's output frame and R2's output frame and rotates both around R2's axis. R2's axis is again expressed in the initial frame. This continues up the chain, each time rotating all the
previous frames around some axis expressed in the initial frame.
Confused already? What's even better is that both ways are exactly equivalent. They are just two ways of looking at the same thing. As I said, chaining rotations are a little counter-intuitive.
The bottom line of it is though if you have a bunch of rotation matrices expressed in the initial reference frame, then the extrinsic view makes the most sense. If you want to first rotate around R1 then R2, etc. then your left-most matrix is the one that you want to rotate around last, i.e., you'd compute Rn*Rn-1*...*R3*R2*R1
Composition of rotation matrix isn't something trivial. I would recommend expressing your rotation matrix as quaternions. Quaternions have very useful properties. Multiplying two quaternions will give a 3rd quaternion which, put back into matrix form, is the exact composition of both input matrix. I think Boost libraries have code for that, but I haven't used it personally.
edit: you can get a rot matrix from (normalized) axis vector and rotation angle. look at this article on wikipedia
I need to divide the points based on their x-position, so that there is, for example, three divisions of points (a middle, left, and right). The middle one should have a range of one meter. Thus, I was wondering what is the min/max ranges of the x-axis? is it large enough to add more divisions than three with same range (1 meter) ?
Thanks
I'm not sure if your question is very precise.
The x and y positions of the depth data will depend in the actual depth of the image. In particular, it will depend on the depth and the angle of the camera. If the wall in front of the camera is very close, there will be less x-axis range.
As an example. For a depth data with an average z-range of 1.5, I get a x-range around [-0.8,0.8]. For another frame, the average z-range is 3.0, the range goes to [-1.6, 1.6]. Of course these numbers depend on the scene itself, it was just to give you a little idea.
Is it clearer now?
If you check the Horizontal field of View up to this equation
Horizontal FOV = 2 * atan(0.5 * width / Fx)
https://developers.google.com/tango/overview/intrinsics-extrinsics
In the Tango yellowstone is about 63 degrees. So it means that you have 31 degrees to left and 31 degrees to right.
Now, if you have pointcloudData based on xyz you can know that if z = 1 meter then you could apply trigonometry
I'm trying to figure out what sort of equation would get me this.
If I have a center node, and an undetermined number of nodes orbiting it, how would I get the canvas coordinates I need to place them at even intervals?
In this case we have eight nodes because they were easiest for me to draw. But in other cases we could have more or less.
If you have a response in Java that would be best. Otherwise pseudocode is fine.
Sample image to help explain:
If you travel from 0,0 to D,0 at an angle of 0 radians, at any other angle A you travel the following distance:
xdiff = D*cos(A)
ydiff = D*sin(A)
(note that A must be in radians - 0 and 2*pi are a full 0/360 degrees, so 2pi/2 is 180 degrees, 2pi/4 is 90 degrees and so on)
So (for example), to draw ten circles at a distance D around a central circle, consider each angle A = i*2pi/10, compute the xdiff and ydiff for each angle, add them to the central circle's position and draw the orbiting circle.
There are a number of approaches.
As a starting point take a look at
http://en.wikipedia.org/wiki/Force-directed_graph_drawing
We have an application where we design a label, and then create an image from the design which is sent to a printer. The image is made into a byte stream before being sent over TCP/IP.
The short story is that we have a rotation setting that automatically calculates the position of the different elements on the label. And it is not working properly.
I was wondering if there is an easy way to calculate position of an element relative to height/width of the containing rectangle (label size). It also needs to take into account that this is a bitmap, so the top left corner is (0,0). This is what confuses me, because most of the rotation algorithms I have found operates on an a regular X/Y axis.
So, say I have a label 500 x 300 (Width x height) with a point (text-box) at 100,100 (upper leftish). I want to rotate this 90 degrees, so that the label is now 300 x 500, with the text-box at 200 x 100 (upper rightish). Note, the rectangle calculation takes care of itself, I just need to find the point relative to the new sides.
I found 180 degrees (width - x, height - y), but having a hard time spotting the algorithm for 90 (and 270)
Probably no rocket surgery involved here, but I have stared myself blind at this problem.
A simple observation can show that for clockwise pi/2 rotation the formula is (hight - y, x). You can arrive to it from the general pi/2 rotation formula, (-y, x). (If you consider that 'origin of coordinates' changes too)
Getting 270-degrees formula is very similar.
If you want to rotate you label counterclockwise by 90 degrees, then each point (x, y) of the label "maps" to (-y, x). In the case of 270 degrees rotation (or 90 degrees clockwise, which is the same) (x, y) becomes (y, -x). Hope this helps.