POV-Ray : Newsgroups : povray.general : Aiming a spaceship Server Time: 19 May 2019 10:53:38 GMT
 Aiming a spaceship (Message 1 to 2 of 2)
 From: Tom A  Subject: Aiming a spaceship Date: 21 Mar 2019 01:30:01 Message:
```Finally got a routine to point my spaceships.  Each is built along the X axis,
in the positive X direction.  I've used a spline to define the spaceships
motion, but getting the ship to point into the direction it is going.
But here it is:

#macro aimAt(p_cur, p_new )
// given an object around origin pointing in the x direction,
// generate a rotation so it can go from p_cur to p_new

#local p_diff = p_new - p_cur; // where it's headed (vector normalized to
origin)
#local y_angle =  -  atan2d(p_diff.z,p_diff.x) ;
#local xy_diff = sqrt(p_diff.x*p_diff.x + p_diff.z*p_diff.z);
#local z_angle =  atan2d(p_diff.y,xy_diff) ;
// #if (p_diff.y < 1) #local z_angle = -z_angle; #end

rotate <0,0,z_angle>
rotate <0,y_angle,0>

#end // aim at

And an example of it's use (cccc is a variable based off of the clock):

object{ Freebooter(1) aimAt( moveSpline(cccc), moveSpline(cccc+.001) )
translate moveSpline(cccc) - <-90,-100,-100>  no_shadow  }

Enjoy!

Tom A.
```
 From: Mike Horvath Subject: Re: Aiming a spaceship Date: 1 Apr 2019 11:45:23 Message: <5ca1f9d3\$1@news.povray.org>
```Does your spaceship adhere to Newtonian physics? In which case your ship
will slide around a lot and not necessarily point in the direction you
are moving.

Mike

On 3/20/2019 9:26 PM, Tom A. wrote:
> Finally got a routine to point my spaceships.  Each is built along the X axis,
> in the positive X direction.  I've used a spline to define the spaceships
> motion, but getting the ship to point into the direction it is going.
> But here it is:
>
> #macro aimAt(p_cur, p_new )
> // given an object around origin pointing in the x direction,
> // generate a rotation so it can go from p_cur to p_new
>
> #local p_diff = p_new - p_cur; // where it's headed (vector normalized to
> origin)
> #local y_angle =  -  atan2d(p_diff.z,p_diff.x) ;
> #local xy_diff = sqrt(p_diff.x*p_diff.x + p_diff.z*p_diff.z);
> #local z_angle =  atan2d(p_diff.y,xy_diff) ;
> // #if (p_diff.y < 1) #local z_angle = -z_angle; #end
>
> rotate <0,0,z_angle>
> rotate <0,y_angle,0>
>
> #end // aim at
>
> And an example of it's use (cccc is a variable based off of the clock):
>
> object{ Freebooter(1) aimAt( moveSpline(cccc), moveSpline(cccc+.001) )
>               translate moveSpline(cccc) - <-90,-100,-100>  no_shadow  }
>
> Enjoy!
>
> Tom A.
>
>
```