Unity 3D Vector calculating on one axis crossing other axis - math

Hey I have a problem and I do not get this calculated on Unity 3D
I want to manipulate vertices. Thats ok but I want to move on X Axis where my mouse is. That doesn't work properly.
So what I do is just throw a ray from origin to a direction. So the ray could be infinitiv long.
With this I want to move the vertice of the mesh to the point where the mouse is. I limited it the range with ray_z = vertice_z (pseudo) but if you look the black line which is the ray you notice it getting longer or shorter when I move or rotate the camera. So the vertice is not on the same position like the mouse.
So I don't get calculated. How can I calculate the position from Z (black line) crosses X (red line).
Example:
cam(1,0,0) // cam & the start position of the ray
x_axis(10,0,10) // red line cutting black
ray_position(15,0,15) // the end of the ray (where the mouse could be if you look from cam to mouse)
Btw: The viewport is not top down. I painted wrong.
If you didn't understand I may try again ^^.

You're looking for Plane.Raycast, I think.
Let's say your plane has a <0, 0, -1> normal (the x-y plane) and passes through the origin:
Plane p = new Plane(Vector3.back, Vector3.zero);
Then you can find the point where a camera/mouse ray intersects with that plane:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distanceAlongRay;
p.Raycast(ray, out distanceAlongRay);
Vector3 mouseOnPlane = ray.GetPoint(distanceAlongRay);
mouseOnPlane is the point on the x-y plane where the mouse ray hit. If you're only interested in the x, then use mouseOnPlane.x.

Related

Formula for calculating camera x,y,z position to force 3D point to appear at left side of the screen and rightmost position on the globe

I'd need a formula to calculate 3D position and direction or orientation of a camera in a following situation:
Camera starting position is looking directly into center of the Earth. Green line goes straight up to the sky
Position that camera needs to move to is looking like this
Starting position probably shouldn't matter, but the question is:
How to calculate camera position and direction given 3D coordinates of any point on the globe. In the camera final position, the distance from Earth is always fixed. From desired camera point of view, the chosen point should appear at the rightmost point of a globe.
I think what you want for camera position is a point on the intersection of a plane parallel to the tangent plane at the location, but somewhat further from the Center, and a sphere representing the fixed distance the camera should be from the center. The intersection will be a circle, so there are infinitely many camera positions that work.
Camera direction will be 1/2 determined by the location and 1/2 determined by how much earth you want in the picture.
Suppose (0,0,0) is the center of the earth, Re is the radius of the earth, and (a,b,c) is the location on the earth you want to look at. If it's in terms of latitude and longitude you should convert to Cartesian coordinates which is straightforward. Your camera should be on a plane perpendicular to the vector (a,b,c) and at a height kRe above the earth where k>1 is some number you can adjust. The equation for the plane is then ax+by+cz=d where d = kRe^2. Note that the plane passes through the point (ka,kb,kc) in space, which is what we wanted.
Since you want the camera to be at a certain height above the earth, say h*Re where 1 < k < h, you need to find points on ax+by+cz=d for which x^2+y^2+z^2 = h^2*Re^2. So we need the intersection of the plane and a sphere. It will be easier to manage if we have a coordinate system on the plane, which we get from an orthonormal system which includes (a,b,c). A good candidate for the second vector in the orthonormal system is the projection of the z-axis (polar axis, I assume). Projecting (0,0,1) onto (a,b,c),
proj_(a,b,c)(0,0,1) = (a,b,c).(0,0,1)/|(a,b,c)|^2 (a,b,c)
= c/Re^2 (a,b,c)
Then the "horizontal component" of (0,0,1) is
u = proj_Plane(0,0,1) = (0,0,1) - c/Re^2 (a,b,c)
= (-ac/Re^2,-bc/Re^2,1-c^2/Re^2)
You can normalize the vector to length 1 if you wish but there's no need. However, you do need to calculate and store the square of the length of the vector, namely
|u|^2 = ((ac)^2 + (bc)^2 + (Re^2-c^2))/Re^4
We could complicate this further by taking the cross product of (0,0,1) and the previous vector to get the third vector in the orthonormal system, then obtain a parametric equation for the intersection of the plane and sphere on which the camera lies, but if you just want the simplest answer we won't do that.
Now we need to solve for t such that
|(ka,kb,kc)+t(-ac/Re^2,-bc/Re^2,1-c^2/Re^2)|^2 = h^2 Re^2
|(ka,kb,kc)|^2 + 2t (a,b,c).u + t^2 |u|^2 = h^2 Re^2
Since (a,b,c) and u are perpendicular, the middle term drops out, and you have
t^2 = (h^2 Re^2 - k^2 Re^2)/|u|^2.
Substituting that value of t into
(ka,kb,kc)+t(-ac/Re^2,-bc/Re^2,1-c^2/Re^2)
gives the position of the camera in space.
As for direction, you'll have to experiment with that a bit. Some vector that looks like
(a,b,c) + s(-ac/Re^2,-bc/Re^2,1-c^2/Re^2)
should work. It's hard to say a priori because it depends on the camera magnification, width of the view screen, etc. I'm not sure offhand whether you'll need positive or negative values for s. You may also need to rotate the camera viewport, possibly by 90 degrees, I'm not sure.
If this doesn't work out, it's possible I made an error. Let me know how it works out and I'll check.

Libgdx - Keeping an object at certain distance and direction from other object

So let's say I have 2 objects. One with the sprite of a circle, other with the sprite of triangle.
My triangle object is set to the position of mouse in every step of the game, while circle is either standing in place or just moving in its own way, whatever.
What I want to do is to have the TRIANGLE move around the circle, but not on it's own, rather on the way your cursor is positioned.
So basically, calculate degree between circle's center and triangle's center. Whenever they are far from each other I just set triangle position to mouse position, BUT when you hover your mouse too close (past some X distance) you can't get any closer (the TRIANGLE is then positioned at maximum that X distance in the direction from circle center to mouse point)
I'll add a picture and hopefully you can get what I mean.
https://dl.dropboxusercontent.com/u/23334107/help2.png
Steps:
1. Calculate the distance between the cursor and the center of the circle. If it is more than the 'limit' then set the triangle's position to the cursor's position and skip to step 4.
2. Obtain the angle formed between the center of the circle and the cursor.
3. Calculate the new Cartesian coordinates (x, y) of the triangle based of off the polar coordinates we have now (angle and radius). The radius will be set to the limit of the circle before we calculate x and y, because we want the triangle to be prevented from entering this limit.
4. Rotate the image of the triangle to 1.5708-angle where angle was found in step 2. (1.5708, or pi/2, in radians is equivalent to 90°)
Details:
1. The distance between two points (x1, y1) and (x2, y2) is sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
2. The angle (in radians) can be calculated with
double angle = Math.atan2(circleY-cursorY, cursorX-circleX);
The seemingly mistaken difference in order of circleY-cursorY and cursorX-circleX is an artefact of the coordinate system in most programming languages. (The y coordinate increases downwards instead of upwards, as it does in mathematics, while the x coordinate increases in concord with math - to the right.)
3. To convert polar coordinates to Cartesian coordinates use these conversions:
triangle.setX( cos(angle)*limit );
triangle.setY( sin(angle)*limit );
where limit is the distance you want the triangle to remain from the circle.
4. In order to get your triangle to 'face' the circle (as you illustrated), you have to rotate it using the libgdx Sprite function setRotation. It will rotate around the point set with setOrigin.
Now, you have to rotate by 1.5708-angle – this is because of further differences between angles in mathematics and angles in programming! The atan2 function returns the angle as measured mathematically, with 0° at three o'clock and increasing counterclockwise. The setRotation function (as far as I can tell) has 0° at twelve o'clock and increases clockwise. Also, we have to convert from radians to degrees. In short, this should work, but I haven't tested it:
triangle.setRotation(Math.toDegrees(1.4708-angle));
Hope this helps!

Rotating a vector in libgdx box2d, firing a cannon ball from a cannon

I have a cannon that fires a cannonball and smoke particle effect, i want the cannon ball to start at the end of the cannon, this i can do by adding the width of the cannon to its x position and adding the half the height to the cannon. This works fine when the cannon is unrotated but when i rotate the cannon its not in the correct position. This is what i use to try and rotate the vector.
Vector2 rotPos = cannon.position.tmp().add(cannon.bounds.width, cannon.bounds.height/2).rotate(cannon.angle);
How can i get a rotated vector that fires the cannon ball in the correct place. See image below.
UPDATE
I tried the below also, same result the ball is off to the left
Vector2 rotPos = world.CannonBody.getWorldVector( world.CannonBody.getPosition() );
The way that you've described the problem, you've solved it for only a single case. This really is just a math problem. Think about the direction you want to shoot, the barrel of the cannon, as the coordinates on a circle.
Since you know the angle, this is easy. Draw a circle with a dot in the center. Then draw a line from the center to the right edge. Then draw another line at a 45 degree angle up from the first line. Connect the two points on the edges with a straight line. You have a triangle now.
Lets call the 45 degree angle line 'r'. And we'll call the first line x, and the last line y.
You should have something that looks like this:
http://i.stack.imgur.com/MJNWZ.jpg
We know that sin(angleInRadians) = y/r. Doing a little algebra we can change this into r*sin(angleInRadians) = y
Boom, you have your y coordinate.
Almost the same thing: cos(angleInRadians) = x/r
So r*cos(angleInRadians) = x
There's your x coordinate.
The you can get the angle of a body directly from box2d, so that's easy. You just need to pick a value for 'r' that represents a correct radius for the circle that you're using to conceptualize the barrel of the cannon at a given angle. If the cannon rotates around the center of the circle, then r is the length of your cannon.
I had an issue which is similar to yours. Here's the question with an answer:
Android. How to move object in the direction it is facing (using Vector3 and Quaternion)
You need something like
translation.set(baseDirection).rot(modelInstance.transform).nor()

Moving a Sphere so its Z-Axis faces a direction

Firstly - Z is Up in this problem.
Context: Top down 2D Game using 3D objects.
The player and all enemies are Spheres that can move in any direction on a 2D Plane (XY). They rotate as you would expect when they move. Their velocity is a 3D vector in world space and this is how I influence them. They aren't allowed to rotate on the spot.
I need to find a formula to determine the direction one of these spheres should move in order to get their Z-Axis (or any axis really) pointing a specified direction in world space.
Some examples may be in order:
X
|
Z--Y
This one is simple: The Spheres local axes matches the world so if I want the Spheres Z-Axis to point along 1,0,0 then I can move the sphere along 1,0,0.
The one that gives me trouble is this:
X
|
Y--Z
Now I know that to get the Z-Axis to point along 1,0,0 in world space I have to tell the sphere to move along 1,1,0 but I don't know/understand WHY that is the case.
I've been programming for ten years but I absolutely suck at vector maths so assume I'm an idiot when trying to explain :)
All right, I think I see what you mean.
Take a ball-- you must have one lying around. Mark a spot on it to indicate an axis of interest. Now pick a direction in which you want the axis to point. The trick is to rotate the ball in place to bring the axis to the right direction-- we'll get to the rolling in a minute.
The obvious way is to move "directly", and if you do this a few times you'll notice that the axis around which you are rotating the ball is perpendicular to the axis you're trying to move. It's as if the spot is on the equator and you're rotating around the North-South axis. Every time you pick a new direction, that direction and your marked axis determine the new equator. Also notice (this may be tricky) that you can draw a great circle (that's a circle that goes right around the sphere and divides it into equal halves) that goes between the mark and the destination, so that they're on opposite hemispheres, like mirror images. The poles are always on that circle.
Now suppose you're not free to choose the poles like that. You have a mark, you have a desired direction, so you have the great circle, and the north pole will be somewhere on the circle, but it could be anywhere. Imagine that someone else gets to choose it. The mark will still rotate to the destination, but they won't be on the equator any more, they'll be at some other latitude.
Now put the ball on the floor and roll it -- don't worry about the mark for now. Notice that it rotates around a horizontal axis, the poles, and touches the floor along a circle, the equator (which is now vertical). The poles must be somewhere on the "waist" of the sphere, halfway up from the floor (don't call it the equator). If you pick the poles on that circle, you choose the direction of rolling.
Now look at the marks, and draw the great circle that divides them. The poles must be on that circle. Look where that circle crosses the "waist"; that's where your poles must be.
Tell me if this makes sense, and we can put in the math.

Math Problem: Getting Coordinates From Position And Angle

let me begin by stating that's i'm dreadful at math.
i'm attempting to reposition and rotate a rectangle. however, i need to rotate the rectangle from a point that is not 0,0 but according to how far its coordinates has shifted. i'm sure that doesn't make much sense, so i've made some sketches to help explain what i need.
the image above shows 3 stages of the red rectangle moving from 0% to 100%. the red rectangle's X and Y coordinates (top left of the red rectangle) only moves a percentage of the blue rectangle's height.
the red rectangle can rotate. focusing only on the middle example ("Distance -50%") from above, where the red rectangle is repositioned at -50 of the blue rectangle's height, its new angle in the above image is now -45º. it has been rotated from its 0, 0 point.
now, my problem is that i want its rotational point to reflect its position.
the red and blue rectangles are the same size, but have opposite widths and heights. since the red rectangle's 0,0 coordinates are now -50% of the blue rectangle's height, and since they have opposite widths and heights, i want the rotational point to be 50% of the red rectangle's width (or 50% of the blue rectangle's height, which is the same thing).
rather than specifically telling the red rectangle to rotate at 50% of its width, in order to do what i want, i need to emulate doing so by using a formula that will position the red rectangle's X and Y coordinates so that its rotational point reflects its position.
Here's an illustrated solution to your problem:
I don't exactly understand what you need, but it seems that a procedure to rotate a rectangle around an arbitrary point may help.
Suppose we want to rotate a point (x,y) d radians around the origin (0,0). The formula for the location of the rotated point is:
x' = x*cos(d) - y*sin(d)
y' = x*sin(d) + y*cos(d)
Now we don't want to rotate around the origin, but around a given point (a,b). What we do is first move the origin to (a,b), then apply the rotation formula above, and then move the origin back to (0,0).
x' = (x-a)*cos(d) - (y-b)*sin(d) + a
y' = (x-a)*sin(d) + (y-b)*cos(d) + b
This is your formula for rotating a point (x,y) d radians around the point (a,b).
For your problem (a,b) would be the point halfway on the right side of the blue rectangle, and (x,y) would be every corner of the red rectangle. The formula gives (x',y') for the coordinates of the corners of rotated red rectangle.
It's quite simple really.
1. Let's settle on your point you want to rotate the rectangle about, i.e. the point of rotation (RP) which does not move when you swivel your rectangle around. Let's assume that the point is represented by the diamond in the figure below.
2. Translate the 4 points so that RP is at (0,0). Suppose the coordinates of that point is (RPx,RPy), therefore subtract all 4 corners of the rectangle by those coordinates.
3. Rotate the points with a rotation matrix (which rotates a point anticlockwise around the origin through some angle which is now the point of rotation thanks to the previous translation):
The following figure shows the rectangle rotated by 45° anticlockwise.
4. Translate the rectangle back (by adding RP to all 4 points):
I assume this is what you want :)
It seems like you could avoid a more complex rotation by more crafty positioning initially? For example, in the last example, position the red box at "-25% Blue Height" and "-25% Red Height" -- if I follow your referencing scheme -- then perform the rotation you want.
If you know the origin O and a point P on the side of rotated rectangle, you can calculate the vector between the two:
(source: equationsheet.com)
You can get the angle between the vector and the x-axis by taking the dot product with this vector:
(source: equationsheet.com)
Given this, you can transform any point on the rectangle by multiplying it by a rotation matrix:
(source: equationsheet.com)

Resources