- Home /
Updating world coordinate according to world points
Hi everybody!
I have found two points lying on a plane, let's say:
worldPoint1 coordinates would be (0,-0.5,1)
worldPoint2 coordinates would be (2.5,-0.5,1)
As far as I understand, the Y coordinates of those two points are identical since they exist on the same plane and are equal to -0.5 since the plane is 0.5m down from the world point where I opened my app. X and Z coordinates are relative to the point I opened the app too.
Now, since I have two points on a plane, I can define my space in an absolute way, right? That way I won't be dependent on the point the camera was when I opened my app and when I "tell" an object to move "right" it will go from worldPoint1 to worldPoint2 no matter where I opened the app and where I stood while clicking the points.
So, what I want to do is aligning the unity's world coordinate system, so the (0,0,0) will be at worldPoint1, (2.5,0,0) will be at worldPoint2 (2.5 cause there is 2.5m distance in the real world between those two points) and up direction will be the normal to the surface.
How can I do that? :)
Thanks!
Do you have the Normal (Vector3) of the surface? Or do you at least have its rotation? As far as I know, you cannot calculate a Normal from the data you've presented.
Is the surface's up axis always aligned with the world's up axis?
Since I use the ground as a plane, the surface's up axis always aligned with the world's up axis. I guess it solves the problem of finding the normal...
Not sure if I understand the question, but if I understand you correctly, the 'plane' is fictional and only exists in your head? the points are already world coordinates and not plane coordinates.
Because the way I read this question is like: I want to have all positions relative to worldPoint1. So that I can put worldPoint1 at 0,0,0 and the rest around it. If that's true, then just substract worldPoint1 from worldpoint2 and you got your desired point...
Vector3 worldPoint1 = new Vector3(0, -0.5f, 1);
Vector3 worldPoint2 = new Vector3(2.5f, -0.5f, 1);
Vector3 worldPoint3 = new Vector3(2.5f, -2.5f, 1);
Vector3 worldPoint4 = new Vector3(0, 2.5f, 1);
Vector3 newpoint1 = Vector3.zero;
Vector3 newpoint2 = worldPoint2 - worldPoint1;
Vector3 newpoint3 = worldPoint3 - worldPoint1;
Vector3 newpoint4 = worldPoint4 - worldPoint1;
Thank you for the answer! I took the idea of your code and now it looks like this:
newPos = newPos - Vector3.Cross(secondWorldPoint - firstWorldPoint, Vector3.up) * 0.01f;
or:
newPos = newPos + (secondWorldPoint - firstWorldPoint) * 0.01f;
It works. I just thought it would be more elegant to rotate the world around a vector than rotate each vector around that vector
Answer by Ady_M · Jan 08, 2019 at 06:05 PM
Maybe this is what you had in mind?
Create an empty object and attach this script.
It will create 3 test objects for you.
Use WSAD to move the green cube according to the red and blue cubes (worldpoint 1 and 2).
As you wrote, moving right goes from worldpoint1 towards worldpoint2.
Moving the red and blue cubes will affect the "right" axis.
// The two world point vectors you mentioned
public Vector3 worldPoint1;
public Vector3 worldPoint2;
// (TEST) These two are used to move the world points around while testing
public Transform worldPoint1_Transform;
public Transform worldPoint2_Transform;
// (TEST)
// This object can be moved using the keys
public Transform movingObject_Transform;
public float movingObjectSpeed = 3;
private void Update ()
{
// You may not need these 2 lines. I had two objects in the scene as world points
worldPoint1 = worldPoint1_Transform.position;
worldPoint2 = worldPoint2_Transform.position;
// Calculate the forward rotation from the two world points
var rotationForward = Quaternion.LookRotation (worldPoint2 - worldPoint1) * Quaternion.Euler (0, -90, 0);
if (Input.GetKey (KeyCode.W))
{
// Move forward
movingObject_Transform.position += rotationForward * Vector3.forward * movingObjectSpeed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S))
{
// Move backward
movingObject_Transform.position += rotationForward * -Vector3.forward * movingObjectSpeed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A))
{
// Move left
movingObject_Transform.position += rotationForward * -Vector3.right * movingObjectSpeed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D))
{
// Move right
movingObject_Transform.position += rotationForward * Vector3.right * movingObjectSpeed * Time.deltaTime;
}
}
private void Awake ()
{
// (TEST)
// Creates 3 test cubes in the scene: "WorldPoint 1", "WorldPoint 2" and "Moving Object"
Transform spawnContainer = new GameObject ("Test Objects").transform;
spawnContainer.parent = transform;
worldPoint1_Transform = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;
worldPoint2_Transform = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;
movingObject_Transform = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;
worldPoint1_Transform.name = "WorldPoint 1";
worldPoint2_Transform.name = "WorldPoint 2";
movingObject_Transform.name = "Moving Object";
worldPoint1_Transform.parent = spawnContainer;
worldPoint2_Transform.parent = spawnContainer;
movingObject_Transform.parent = spawnContainer;
worldPoint1_Transform.position = new Vector3 (0, 0, 5);
worldPoint2_Transform.position = new Vector3 (5, 0, 0);
movingObject_Transform.position = new Vector3 (0, 0, 0);
worldPoint1_Transform.GetComponent <Renderer> ().material.color = Color.red;
worldPoint2_Transform.GetComponent <Renderer> ().material.color = Color.blue;
movingObject_Transform.GetComponent <Renderer> ().material.color = Color.green;
}
AWESO$$anonymous$$E! It works really good! Thank you very much for arranging my code and more important- my thoughts :)