NAME
Games::3D::Vector - a vector in 3D space
SYNOPSIS
use Games::3D::Vector;
my $vec = Games::3D::Vector->new( x => 1, y => 9, z => -1 );
$vec->scale(5);
$vec->normalize();
print $vec->length();
$vec->flip(5); # same as scale(-1)
EXPORTS
Exports nothing on default.
DESCRIPTION
This package provides a base class for vectors in 3D space, including the ability to manipulate (rotate, translate, scale, normalize) them.
METHODS
- new()
-
my $vector = Games::3D::Vector->new( $arguments );
Creates a new vector. The arguments are the x,y and z components like:
my $vector = Games::3D::Vector->new( x => 9, y => -8.1, z => -2);
- id()
-
Return the vectors's unique id.
- x()
-
print $vector->x(); $vector->x(123);
Set and return or just return the vector's X component.
- y()
-
print $vector->y(); $vector->y(123);
Set and return or just return the vector's Y component.
- z()
-
print $vector->z(); $vector->z(123);
Set and return or just return the vector's Z component.
- pos()
-
print join (" ", $vector->pos()); $vector->pos(123,456,-1); # set X,Y and Z $vector->pos(undef,undef,1); # set only Z
Set and return or just return the vector's components.
- length()
-
$vector->length();
Returns the vectors length.
- normalize()
-
$vector->normalize();
Normalizes the length of the vector to
1.0
. - scale()
-
$vector->scale($factor);
Scales the vector by this factor.
- flip()
-
$vector->flip();
Makes the vector point backward, e.g. is equivalent to
$vector-
scale(-1);>. - translate()
-
$vector->translate($x,$y,$z);
Translates the vector in X, Y and Z direction. Undef values are the same as 0, e.g. leave that axis alone:
$vector->translate(1,undef,2);
is the same as:
$vecotr->translate(1,0,2);
- rotate()
-
$vector->rotate($xr,$yr,$zr);
Rotates the vector around the X, Y and Z axis (in that order). The arguments are expected to be passed in radians.
- copy()
-
$vector_copy = $vector->copy();
Make a copy of the vector object and return it.
- cross()
-
$vector->cross($vector2);
Computes the cross product of
$vector
and$vector2
and stores the result in$vector
. If you want just the cross product without modifying the first vector, use:$cross = $vector->copy()->cross($vector2);
- dot()
-
$dot = $vector->dot($vector2);
Computes the dot product of
$vector
and$vector2
and returns the result as a scalar. - set()
-
$vector->set($x,$y,$z);
Set the vector to the three components.
- add()
-
$self->add($other);
Add another vector to the first one.
- mul()
-
$self->mul($x,$y,$z);
Multiply X,Y,Z components with
$x
,$y
,$z
, respectively. - subtract()
-
$self->subtract($other);
Subtract another vector from the first one.
- divide()
-
$self->divide($scalar);
Divide the vector by a scalar (can be seen as the inverse of scale);
AUTHORS
(c) 2003, Tels <http://bloodgate.com/>
SEE ALSO
Games::3D::Point, SDL:App::FPS, SDL::App and SDL.