There are the planet and a few satellites in 3d space. I need to calculate axis of rotation for every satellite. They should rotate around planet center.
I calculated the vector from satellite to the center of the planet.
vec1 = planetCenter - sputnikCenter;
With vec1 and planetCenter I can calculate the equation of plane, that perpendicular to vec1.
equation like that:
A.x + B.y + C.z + D = 0
Now, I should get random vector on this plane. This vector will be axis of rotation. But how can I get this random vector?
Using Barycentric Coordinates To Find A Random Point On A Plane. As we will get help from the barycentric coordinates of a triangle to find a random point inside the plane, we can simply divide the plane into two triangles using two of the opposing corners.
Vector3. ProjectOnPlane uses the two Vector3 values to generate the position of vector in the planeNormal direction, and return the location of the Vector3 on the plane. The vector starts at the red circle and ends at the black circle representing the plane normal.
well if you got the plane A.x + B.y + C.z + D = 0
then n(A,B,C)
is the normal vector. So I think the easiest approach to your task is to use basis vectors. So you need 2
perpendicular vectors on this plane. For that you can exploit cross product. first some definitions:
knowns:
p
planet center position (or the center point of your rotations or any point on the plane so in worst case you can try p=0,0,-D/C
or any other combinationn...)n
normal vectorq= (1,0,0) or (0,1,0)
chose the one that has lesser |dot(n,q)|
operations:
vector = cross(a,b) = a
x b
- cross product returns perpendicular vector to a,b
scalar = dot(a,b) = (a
.b)
- dot product returns 0
if a,b
are perpendicular|a| = abs(a)
- absolute value (both scalar and vector)scalar = Rand()
- float pseudo random value on interval <0.0,1.0>
unknowns:
u,v
- basis vectors
r
- your pseudo-random pointSo first get u,v
by exploiting cross product:
u=cross(n,q)
v=cross(n,u)
And now the point:
r = p + u*(2.0*Rand()-1.0) + v*(2.0*Rand()-1.0)
If you want just random vector then ignore the start position p
r' = u*(2.0*Rand()-1.0) + v*(2.0*Rand()-1.0)
That is all ... so you can compute u,v
once (per normal vector change) and generate the r
as often as you need. If u,v
are unit vectors then this will generate points inside 2x2
square ... if you want more or less just add scales to them ...
see Is it possible to make realistic n-body solar system simulation? and generate random orbital parameters for Kepler's equation instead ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With