Convert distance to rotation

Started by blinkok, September 03, 2020, 22:52:42

Previous topic - Next topic

blinkok

I have an object moving around a sphere. It is pointing in a particular direction and traveling at a particular speed.
How do i convert that angle and speed to an x/y/z angle of rotation?

I know, it's high school algebra, but any help is greatly appreciated.

Thanks

TomToad

First, is it 2D or 3D? 

2D is easy.  X = Cos(Angle) * speed; Y = Sin(Angle) * speed. 

3D is more complicated as you need to know what conventions are used. with speed r,  Inclination θ, and azimuth φ from spherical coordinates

x = r * sin θ * cos φ
y = r * sin θ * sin φ
z = r * cos θ

Using cylindrical coordinates where p is the distance from z axis, azimuth φ, and height z,

x = p * cos φ
y = p * sin φ
z = z




------------------------------------------------
8 rabbits equals 1 rabbyte.

blinkok

Ok thanks. I think i can work with that

Matty

May ask a dumb question? If you already have an object moving around, or constrained to, movement on a sphere then don't you already know it's x,y,z speed given you are moving the object already?

blinkok

I only know it's forward velocity. I want to convert that forward velocity to and X/Y/Z rotation in order to move the object around the sphere

Kryzon

You would only need two rotation axes, say X and Y, to "aim" that 3D rotation around the surface of the sphere.

Start with a vector that goes from the world-space center of the sphere to the future world-space position of the object -- that is, the location of the object after it's moved. Then you can get the "pitch" and "yaw" angles of this vector in the same way that Blitz3D did it:
- https://github.com/blitz-research/blitz3d/blob/master/bbruntime/bbblitz3d.cpp#L1651-L1657
- https://github.com/blitz-research/blitz3d/blob/master/blitz3d/geom.h#L108-L113

You can do the same with the current position of the object if you need to get the difference of angles rather than absolute angles.

blinkok

Quotethat is, the location of the object after it's moved
I don't know that. I have the objects x/y/z position, the direction it is facing and the velocity.

Kryzon

#7
That's all you need, it's vector displacement.
futurePosition = objectPosition + directionVector * velocityThis works whether the vectors are 2D or 3D, with 'velocity' being a scalar (a number) and 'directionVector' being normalized (1 unit in length). 

You then subtract the position of the center of the sphere from 'futurePosition' so you have a vector going from the center to future position, and it's this vector that you get the yaw and pitch angles from. 

Edit: in case you only have the object's rotation and not 'directionVector', you can get 'directionVector' by transforming a (0, 0, +1) vector from the object's space to world space:directionVector = objectMatrix * Vector(0, 0, 1)With that being a 3D vector multiplied by a 4x4 transformation matrix (most libraries temporarily turn that 3D vector into 4D for this to work, so it's (0,0,1,W), with W=1, the multiplication by the matrix is done, then it returns just the first three components as you expect, XYZ).

blinkok

QuotefuturePosition = objectPosition + directionVector * velocity

Doesn't that give me a position in a straight line?

Kryzon

That's not the end of it. Like I said, you are going to get the vector from the center-of-the-sphere to that futurePosition, and get the rotation angles of the vector that results from that. 

You'll be converting futurePosition into two angles, horizontal and vertical (or pitch & yaw, or X & Y), around the sphere.

Kryzon

Here, that formula is called the "vector equation of a line": 
http://sites.science.oregonstate.edu/math/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/lineplane/lineplane.html

In there, 'objectPosition' is "r0" (a point), the 'directionVector' is "v" and the 'velocity' is "t" (the "time", a scalar that says how much you traveled with that velocity vector).

blinkok


3DzForMe

I'd hiccups trying to do a similar thing in Blitz3D for a 3d space invaders thing.... I admitted defeat  :D
BLitz3D, IDEal, AGK Studio, BMax, Java Code, Cerberus
Recent Hardware: Dell Laptop
Oldest Hardware: Commodore Amiga 1200 with 1084S Monitor & Blitz Basic 2.1