Home > database >  Is there a way to determine the new position of entity E, given angles and distance in higher than 3
Is there a way to determine the new position of entity E, given angles and distance in higher than 3

Time:06-18

I want to be able to take a given Entity E, it’s current facing angles, it’s origin position, and then find the coordinates of a future point given a distance. I know there are many ways to do it in 2d and 3d, but I want to figure it out for any N dimensions, where N > 3.

CodePudding user response:

Yes. If I understand correctly, you are converting velocity expressed in n-dimensional spherical coordinates to n-dimensional cartesian coordinates. The formula there is

x_1 = r×cos(a_1)

x_2 = r×sin(a_1)×cos(a_2)

x_i = r×sin(a_1)×sin(a_2)×...×sin(a_{n-1})×cos(a_i)

x_n = r×sin(a_1)×...×sin(a_{n-1})

Where x_1,...x_n are the cartesian coordinates of the velocity vector, r is the speed (magnitude of the velocity) multiplied by the time, and a_1,... a_{n-1} are the facing angles in radians.

Once you've done the conversion, it's simply a matter of adding the displacement vector x_1,...x_n with the position component wise.

So lets say you are working in 6d space. A simple Python program for this calculation might look like this:

import math

'''
Get the new position of an object moving with constant velocity in 6d space after a given time given it's original position, facing, and speed.

Params:
start_pos: A tuple of 6 floats representing the start position in 6d space
facing: a tuple of 5 floats, representing the facing angles the object as angles in radians. Equivalent to yaw and pitch in 3d
speed: float representing the speed of the object
t: the amount of time
time: 
'''
def position(start_pos, facing, speed, t):
    distance = speed*time
    # calculate the displacement in each of the 6 coordinate directions using the spherical-to-cartesian formulas
    dis0 = distance*math.cos(facing[0])
    dis1 = distance*math.sin(facing[0])*math.cos(facing[1])
    dis2 = distance*math.sin(facing[0])*math.sin(facing[1])*math.cos(facing[2]
   dis3 = distance*math.sin(facing[0])*math.sin(facing[1])*math.sin(facing[2])*math.cos(facing[3])
   dis4 = distance*math.sin(facing[0])*math.sin(facing[1])*math.sin(facing[2])*math.sin(facing[3])*math.cos(facing[4])
   dis5 = math.sin(facing[0])*math.sin(facing[1])*math.sin(facing[2])*math.sin(facing[3])*math sin(facing[4])
  # Add the displacement in each direction to the original position to get the new position. 
  new_position = (position[0]   dis[0], position[1]   dis1, position[2]   dis2, position[3]   dis3, position[4]   dis4, position[5]   dis5)
  return new_position

CodePudding user response:

You can simply use parametric hypersphere/nsphere equation for this just like the other answer suggest...

hyper sphere parametric equation

however if you plan to do more in ND its better to switch to transform matrices...

For this task You can use transform matrices and Euler Angles ...

However note that number of angles is the number of unique axis pairs (as rotation is determined by rotation plane instead of rotation axis which is just our human abstraction that works only in 3D)...

2D: xy 
3D: xy,xz, yz
4D: xy,xz,xw, yz,yw, zw

for higher than 4D is usual to name the axises x1,x2,x3,x4,x5,...

5D: x1x2,x1x3,x1x4,x1x5, x2x3,x2x4,x2x5, x3x4,x3x5, x4x5

Now you just need to construct N 1 dimensional transform matrices for example see:

its just expansion from:

the same can be applied for translation.

So for example in 4D Euler angles code would be:

p' = Rxy * Rxz * Rxw * Ryz * Ryw * Rzw * T * p

Where R?? are rotational matrices, T is translation matrix (your origin) and p is point to transform. Note that order of matrix multiplication is matter of notation ... Now if your angles are representing orientation of x as major axis then:

p' = Rxy * Rxz * Rxw * Ryz * Ryw * Rzw * T * vec5(distance,0,0,0,1)

The last homogenuous coordinate W=1 (do not confuse it with w axis) means that we are transforming position so T is used, for directions use w=0 that will bypass T ...

Also beware Euler angles usage always leads to singularities so expect weird behavior on some orientations !!!

You can also forget about the angles and describe your orientation and position with single matrix instead (that has many advantages) ...

To handle ND coding obstacles like nested loops you can use either recursion or something like this:

look for nested_for function in code ... its simple to use equivalent to N nested for loops where N can change in runtime:

for (...)
 for (...)
  ...
   for (...)
     {
     ...
     }
  • Related