Ooops
September 29, 2020, 02:33:31 PM

Author Topic: Convert distance to rotation  (Read 282 times)

Offline blinkok

  • Full Member
  • ***
  • Posts: 244
Convert distance to rotation
« on: September 03, 2020, 10:52:42 PM »
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

Offline TomToad

  • Hero Member
  • *****
  • Posts: 513
Re: Convert distance to rotation
« Reply #1 on: September 03, 2020, 11:26:56 PM »
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.

Offline blinkok

  • Full Member
  • ***
  • Posts: 244
Re: Convert distance to rotation
« Reply #2 on: September 04, 2020, 12:13:02 AM »
Ok thanks. I think i can work with that

Offline Matty

  • Hero Member
  • *****
  • Posts: 1172
    • MattiesGames
Re: Convert distance to rotation
« Reply #3 on: September 04, 2020, 12:17:27 AM »
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?

Offline blinkok

  • Full Member
  • ***
  • Posts: 244
Re: Convert distance to rotation
« Reply #4 on: September 04, 2020, 12:50:17 AM »
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

Offline Kryzon

  • Full Member
  • ***
  • Posts: 230
Re: Convert distance to rotation
« Reply #5 on: September 04, 2020, 12:36:53 PM »
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.

Offline blinkok

  • Full Member
  • ***
  • Posts: 244
Re: Convert distance to rotation
« Reply #6 on: September 05, 2020, 12:07:14 AM »
Quote
that 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.

Offline Kryzon

  • Full Member
  • ***
  • Posts: 230
Re: Convert distance to rotation
« Reply #7 on: September 05, 2020, 08:44:34 AM »
That's all you need, it's vector displacement.
Code: [Select]
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:
Code: [Select]
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).

Offline blinkok

  • Full Member
  • ***
  • Posts: 244
Re: Convert distance to rotation
« Reply #8 on: September 05, 2020, 10:15:36 AM »
Quote
futurePosition = objectPosition + directionVector * velocity

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

Offline Kryzon

  • Full Member
  • ***
  • Posts: 230
Re: Convert distance to rotation
« Reply #9 on: September 05, 2020, 12:48:04 PM »
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.

Offline Kryzon

  • Full Member
  • ***
  • Posts: 230
Re: Convert distance to rotation
« Reply #10 on: September 05, 2020, 12:53:00 PM »
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).

Offline blinkok

  • Full Member
  • ***
  • Posts: 244
Re: Convert distance to rotation
« Reply #11 on: September 05, 2020, 10:18:29 PM »
Ok. Thanks for that

Offline 3DzForMe

  • Hero Member
  • *****
  • Posts: 1109
Re: Convert distance to rotation
« Reply #12 on: September 06, 2020, 05:04:28 PM »
I'd hiccups trying to do a similar thing in Blitz3D for a 3d space invaders thing.... I admitted defeat  :D

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal