POV-Ray : Newsgroups : povray.advanced-users : Point at a vector; angles for rotation on axes X and Y /w nonzero origin Server Time
31 Oct 2024 14:11:43 EDT (-0400)
  Point at a vector; angles for rotation on axes X and Y /w nonzero origin (Message 1 to 4 of 4)  
From: Mike C
Subject: Point at a vector; angles for rotation on axes X and Y /w nonzero origin
Date: 16 Aug 2005 13:40:00
Message: <web.43022440433cdf6544365ed70@news.povray.org>
Hi all,

First, apologies in advance if this is in the wrong group or previously
posted -- I tried searching and looking in the archives, and couldn't find
anything.  Maybe I didn't search hard enough.

Anyways, I have two objects, one a cylinder pointing at positive Z, and a
semisphere pointing at Y with a cutout of it holding above said cylinder
(although it's "front" is at Z for the purpose of this discussion).  These
are translated off of the origin.

[Anyone who is interested, the object is supposed to represent a sort of
cannon or gun on a spaceship or other ship.]

Anyways, I want to rotate the cylinder (barrel of the gun) on the X and Y
axes to point at a Vector, and I want the associated semisphere ("body" of
the gun, I suppose?) to rotate the same amount on the Y axis, but not to
rotate in any other way.  These calculations, I'm hoping, should be based
on the vector of the cylinder's location (which could be stored in a
seperate variable), as opposed to the origin, or at least, when translated,
will end up pointing at the intended destination.

There will be two sets of objects (handled independantly), both pointing at
the same vector from two different locations.

I am aware of the Point_At_Trans and Reorient_Trans, but I can't get them to
work properly; either the objects end up pointing the wrong way, or they
don't translate to the spot I want them in.

Hopefully this makes sense; my thinking is a tad unclear today.

Thanks in advance for any response.

~Michael Chang


Post a reply to this message

From: Mike C
Subject: Re: Point at a vector; angles for rotation on axes X and Y /w nonzero origi=
Date: 16 Aug 2005 14:35:01
Message: <web.430230a473034d4744365ed70@news.povray.org>
I believe I've found a solution, but if anyone has a better method, let me
know or post a message.

Requires 'include "transforms.inc"'.

    #macro Point_At_Trans_MyO(MOrigV,Target)
      translate -MOrigV
        #local Z = vnormalize(Target-MOrigV);
        #local X = VPerp_To_Vector(Z);
        #local Y = vcross(X, Z);
        Shear_Trans(X, Y, Z)
      translate MOrigV
    #end

Where positive Z is the front of the object (the part you want to face the
target), "MOrigV" is the vector of the object, and "Target" is the vector
you want to point at.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Point at a vector; angles for rotation on axes X and Y /w nonzero origi=
Date: 16 Aug 2005 18:05:01
Message: <web.430261e273034d4752d573c20@news.povray.org>
"Mike C" <the### [at] hotmailcom> wrote:
> Hi all,
>
> First, apologies in advance if this is in the wrong group or previously
> posted -- I tried searching and looking in the archives, and couldn't find
> anything.  Maybe I didn't search hard enough.
>
> Anyways, I have two objects, one a cylinder pointing at positive Z, and a
> semisphere pointing at Y with a cutout of it holding above said cylinder
> (although it's "front" is at Z for the purpose of this discussion).  These
> are translated off of the origin.
>
> [Anyone who is interested, the object is supposed to represent a sort of
> cannon or gun on a spaceship or other ship.]
>
> Anyways, I want to rotate the cylinder (barrel of the gun) on the X and Y
> axes to point at a Vector, and I want the associated semisphere ("body" of
> the gun, I suppose?) to rotate the same amount on the Y axis, but not to
> rotate in any other way.  These calculations, I'm hoping, should be based
> on the vector of the cylinder's location (which could be stored in a
> seperate variable), as opposed to the origin, or at least, when translated,
> will end up pointing at the intended destination.
>
> There will be two sets of objects (handled independantly), both pointing at
> the same vector from two different locations.
>
> I am aware of the Point_At_Trans and Reorient_Trans, but I can't get them to
> work properly; either the objects end up pointing the wrong way, or they
> don't translate to the spot I want them in.
>
> Hopefully this makes sense; my thinking is a tad unclear today.
>
> Thanks in advance for any response.

Mike,
I've prepared some code for you below that shows how you can use the
Reorient_Trans() macro to point your barrel at a target.


Tor Olav
http://subcube.com

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.6;

#include "colors.inc"
#include "transforms.inc"

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare pTarget = <-10, 5, 7>;
#declare pBase = <1, 1, -3>;
#declare pFront = pBase + 3*z;

#declare vFrom = pFront - pBase;
#declare vTo = pTarget - pBase;

#declare Target = sphere { pTarget, 0.2 }
#declare Base = sphere  { pBase, 0.3 }
#declare GunBarrel = cylinder { pBase, pFront, 0.2 }
#declare LineOfSight = cylinder { pBase, pTarget, 0.1 }
#declare ReorientedGunBarrel =
  object {
    GunBarrel
    translate -pBase
    Reorient_Trans(vFrom, vTo)
    translate pBase
  }

object { Target pigment { color Cyan } }
object { Base pigment { color Orange } }
object { GunBarrel pigment { color Yellow } }
object { LineOfSight pigment { color White } }
object { ReorientedGunBarrel pigment { color Red } }

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

light_source { <1, 2, -2>*100 color White*2 }

camera {
  location <3, 1, -1>*4
  look_at pBase
}

background { color Gray40 }

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Point at a vector; angles for rotation on axes X and Y /w nonzero origi=
Date: 16 Aug 2005 18:25:00
Message: <web.4302672e81614d0952d573c20@news.povray.org>
The same result can be achieved if you add this code after the
"vTo"-calculation:

  #declare vAxis = vnormalize(vcross(vFrom, vTo));
  #declare Angle = acos(vdot(vnormalize(vFrom), vnormalize(vTo)));
  #declare RotationAmount = 1.0; // Try other values here (between 0 to 1)

- and then replace this statement:

  Reorient_Trans(vFrom, vTo)

- with this one:

  Axis_Rotate_Trans(vAxis, degrees(Angle)*RotationAmount)

--
Tor Olav
http://subcube.com


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.