A point in 3D space from an angle - c#

I'm currently working on a camera for a game. But I got stuck at the rotation.
When I move my mouse across the x- or y-axis, I want the camera to rotate around my character.
What would be a formula to calculate this vector, if the distance to the character is always the same?
I am doing this in Unity, with C#, if this is of any help.

this function may help: transform.RotateAround(Vector3 axis, float degree)
you can read the Unity Script Reference for more info.
-oh and I think you should tag your next questions with "unity3d"
but you get the best Unity3d-help at UnityAnswers-Forum http://answers.unity3d.com/index.html.

You can utilise spherical coordinates — they seem to fit more than Euler angles for the purpose of camera movement. The Cartesian vector which you need can be obtained by simple formulas as described there.

Related

ScreenPointToRay, What's behind the function?

I'd like to know what code is behind Camera:ScreenPointToRay may it be Unity or ROBLOX.
My situation is that I have a position in WorldSpace and the camera's ViewportSize,
I'd like to have a function that does ScreenPointToRay,
I'm assuming you'd only need those two parameters to create this function but I have no clue how.
May someone spoon-feed me on this?
There are several ways you can do this calculation. This version will only work for perspective cameras, but can be modified to work on orthographic cameras as well. Unfortunately I an unable to share code, but I will try to explain as best I can.
From your camera, you will need to generate a Matrix4x4 by inverting the result of multiplying the Camera.projectionMatrix by the Camera.worldToCameraMatrix.
You will need to convert your screen space pixel into a clip plane coordinate. In the clip plane -1,-1 is equivalent to 0,0, and 1,1 is equivalent to Camera.pixelWidth, Camera.pixelHeight.
Next you will use the Matrix4x4.MultiplyPoint function to multiply your clip point.
The direction of your ray is the result from step 3 minus the camera's position.
Construct the ray using the direction from step 4 and use the camera's position as the ray origin.
If you are trying to do this outside of the Unity API, the Matrix4x4.MultiplyPoint function will need to created as well.
You can check this article, it goes through using the matrices available on the Camera of Unity to create rays for all pixles inside a compute shader
The Roblox API has a section on the function you're looking for: ScreenPointToRay.
The Unity Scripting Reference also has a section their own ScreenPointToRay function.

How can I find the rotation of the head in Google Cardboard Unity3D?

Hey guys how do I find the rotation of the phone with Google Cardboard SDK in Unity3D, like the way the persons head is facing? I need to find if it is facing more towards the east, the west, or the north. Do I find the rotation of the head, or the parent Main camera?
The Cardboard class contains a property called HeadRotation which is a Quaternion.
Quaternion crtRot = youCardboardController.HeadRotation;
To use its rotation like you'd in Unity with directional vectors you may simply multiply it by Vector3.forward.
Vector3 lookDir = crtRot * Vector3.forward;
Visualizing the vector in editor might help narrow down issues
void Update () {
// ..
Debug.DrawRay( pos, lookDir * 100, Color.blue );
// ..
}
From here you only need to know where North is, in meaning of the facing vector. Do a Vector3.Angle() and you have the angle between your avatar's and North's vector. You might want to evaluate both vectors with their y axes set to 0.
You could also use Vector3.Dot() (the dot product) to determine how much both vectors look into the same direction.
I had an issue with HeadPosition, which wasn't updated properly. So if you operate from HeadPosition keep in mind it may stick to 0,0,0 .
Take Head.transform.rotation - that's the orientation of the user's head.
I'm not really sure what do you mean by North or West. If the mobile device has a magnetometer, then maybe you could read it's data to determine where North is, but I've never done that
It would be the rotation of the Head object, although the Main Camera usually has the same rotation.
But if you want the direction relative to the real world, that information is not available in the SDK. The compass cannot be used because of the magnet, so there is no way to get the real direction.
Just take the rotation angle of the main camera in the scene. It will gives you the head rotation angles with respect to x, y and z axis.
In GVR based VR scenes the system is internally rotating the camera with respect to our head movements. So it's easy to get the head rotation using main camera rotation angles.
For that,
//local rotation of the Transform of the camera
Camera.main.transform.localEulerAngles.y
//The world Y rotation
Camera.main.transform.rotation.y

I have two points specified as lat/long(s) and would like to insert a new point in between them

I have two points specified as lat/long(s) and would like to insert a new point in between them a certain distance (in meters) from point the initial point.
Presumably I convert the lat/longs to radians and then use a version of midpoint formula?
How can i accomplish this task?
I'm living in c# land btw.
TIA
The exact solution is not that simple (an approximate solution is just given by linear interpolation). You're not going to love it.
The trajectory follows a great circle, which is the intersection between the sphere and a plane through the center and the two given points.
First convert from spherical to Cartesian coordinates. Then by the cross product of the vectors from the center to the two points, determine the direction of the normal to the plane (normalize the vector). Then use the formula for 3D rotation around this axis and rotate the starting point. The rotation angle is given by the desired distance divided by the radius of the Earth. Finally, convert back to spherical coordinates.

Rotate an object according to a plane normal

I am making a FPS game, I created a peaceful AI for the moment and when the character is died, I just want it to be oriented according to the normal below it. I show you the result for the moment :
as you can see, the character is flying, because the terrain is not straight.
I am trying (without success) to make something like that :
I have the (x,y,z) coordinates (character position) and the normal to the plane.
Yes the normal is always facing up, as you can see on my drawing, even if I drew it in the good pose. I dont understand when you are talking about the quaternion, which new normal are you talking about ?
I already have the normal of the plane under the character, so, part of the jobs is already done :)
Your character's death pose is always with the normal facing up, right?
And you want to rotate it to another normal.
You can find a Quaternion that represents the rotation between two vectors (up and the new normal) to rotate the mesh.
This question has the answer to that:
Quaternion from two vector pairs
To know the normal of your terrain, you can probably cast a ray down and get the normal from the collision information depending on the physics engine you are using.
Edit:
Some background info:
A quaternion represents a rotation. So what I'm suggesting is that you use the answer from the question I linked to calculate the rotation between the default orientation of the character's death pose (UP) to the new orientation (Terrain Normal)
Then you can just transform your dead character with the Quaternion and it'll follow the terrain's normal.
Here's a sketch: calculate Q from UP and N with the solution from the Link and rotate your character's model with Q:

Is there a 3D equivalant to clamp in XNA?

I'm building a 3D game but i only plan on using a 2D perspective thus not taking the z axis into the equasion,
i want to be able to limit the movement of one of my models so it doesn't move out of the unmoving field of view,
when i was designing 2D it was simple just use clamp, but i cant seem to figurebout how to do this in 3d
any help would be much appreciated
Regards
Just use Vector3.Clamp(Vector3 value1, Vector3 min, Vector3 max) and use the constructor on Vector3 that takes a Vector2 and an int (for z value).
If you simply want to do the same thing as a 2D clamp would - Vector3.Clamp set the Y (usually 'UP') component of the two bounding vectors you pass to be 0.
I'm slightly confused on the question however, it seems that maybe what you are after is a form of collision detection with the view frustum This article may help with that if your model can fit into a bounding sphere relativity nicely.
You will need to for test collision vs all the planes which define the view space. If its a perspective camera your using, you'll need to get the Frustum Planes, otherwise if its an orthographic camera they are the planes that make the bounding box of the view space (a cuboid which is orientated the same way as the camera).
So you want in pseudocode:
if (object.position+movementVec is in view of camera)
object.position+=movementVec;

Categories

Resources