0

I've been defining a plane using a position and a size in the X and Z axis and then getting a random position within that plane in order to make characters to traverse randomly within.

So far I didn't need to change the orientation so to get a random position within the plane was rather simple.

However now I need to rate the plane (in the Y axis) to accommodate it to the terrain, and I would like to know how to keep getting a random position within a rotated plane?

Many thanks!

Cris
  • 197
  • 1
  • 4
  • 16
  • If you want your question reopened, [edit](https://stackoverflow.com/posts/62151287/edit) your question so others can understand it. Do not put an answer in your question. If your question is reopened then you can answer it yourself, or accept an answer. – Dour High Arch Jun 04 '20 at 16:48
  • Hi Dour High Arch, I've been trying to edit twice to make it clearer or provide information to others that might get the same problem as me (and lack the knowledge). I am not a native English speaker but to me the question sounds rather clear (to the point that someone actually understood it and answer it before it was closed). If you still think is not clear, please could you provide me some feedback as per what part you or others don't understand. Really appreciate your feedback so I can improve in the future. – Cris Jun 04 '20 at 20:32
  • The most important thing is to [show your code](https://idownvotedbecau.se/nocode/). It's effectively impossible to fix code you haven't shown us. Would the answer given be helpful if it included no code? Note that the person answering did not actually understand what you wanted. – Dour High Arch Jun 05 '20 at 00:49
  • I see... thanks for your input, will follow your advise in my future questions. – Cris Jun 05 '20 at 15:14

2 Answers2

0

If you just rotate it around the Y-axis, then you still get a random position within the original plane boundaries, then rotate it accordingly with 2d rotation formulas.

What it means is that you multiply your Vector3 by a rotation matrix, or you just apply the fomula. So assuming you have your random Vector3 position (called pos in my code) generated and you want to apply a rotation of theta , your code could be :

Vector3 newPosition = new Vector3(pos.x * Math.cos(theta) - pos.z * Math.sin(theta), pos.y, pos.x * Math.sin(theta) + pos.z * Math.cos(theta));

I haven't tested it but it should work.

  • Thanks Monique, I've tried your code but it didn't work in my case. I believe I didn't properly explained what I was trying to do. But in any case, your answer helped me research for the solution. Appreciated your time :) – Cris Jun 04 '20 at 14:45
0

Realized what I needed is to understand how to rotate a point around another point, this post helped a lot to realize the answer: Rotate a point around another point

Here is the solution if anyone is interested: Assuming I have a plane consisting of a center position, and a size in X and a size in Z, I generate a random position within:

float randomX = Random.Range(centerPos.position.x - sizeHalfX, cCenterPos.position.x + sizeHalfX);
float randomZ = Random.Range(centerPos.position.z - sizeHalfZ, centerPos.position.z + sizeHalfZ);

Vector3 randPos = new Vector3(randomX, centerPos.position.y, randomZ);

Now I have a random position within a plane which is no rotated in the Y axis. So once we have the rotation in Y (assuming it is degrees), we can apply it to the random position, so the random position will fall within the rotated plane:

float angleInRadians = -(rotation * (float)(Math.PI / 180.0d));
float cosTheta = (float)Math.Cos(angleInRadians);
float sinTheta = (float)Math.Sin(angleInRadians);

float posX = (cosTheta * (randPos.x - centerPos.position.x) - sinTheta * (randPos.z - centerPos.position.z) + centerPos.position.x);
float posZ = (sinTheta * (randPos.x - centerPos.position.x) + cosTheta * (randPos.z - centerPos.position.z) + centerPos.position.z);

Vector3 finalPos = new Vector3(posX, centerPos.position.y, posZ);

Please note "centerPos" refers to the center position of the plane.

Cris
  • 197
  • 1
  • 4
  • 16