www.pudn.com > j3dme-0.3.0.rar > Coordinate.java


/*
* J3DME Fast 3D software rendering for small devices
* Copyright (C) 2001 Onno Hommes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

package net.jscience.j3dme;

/**
* This class provides the engine 3D coordinate transformations,
* translations and scaling functions. All the calculations use
* fixed point integers. A coordinate object is typically used
* as a cache for coordinate transformations. Coordinates
* can only be expressed as natural numbers (e.g. {1,4,7})
*/
public class Coordinate{

/**
* Cache values for rotation
* sin and cosine values for pitch, yaw and roll
*/
private int sp,cp,sy,cy,sr,cr;

/**
* The x component of the coordinate
*/
public int x;

/**
* The y component of the coordinate
*/
public int y;

/**
* The z or depth component of the coordinate
*/
public int z;

/**
* The coordinate constructor.<p>
* The coordinate components will all be initialized to 0.
*
* @return a coordinate object {0,0,0}
*/
public Coordinate(){
x = 0;
y = 0;
z = 0;
sp = 0;
sy = 0;
sr = 0;
cp = 256;
cy = 256;
cr = 256;
}

/**
* Another coordinate constructor.<p>
* The coordinate components will be initialized to the provided
* x , y and z values.
*
* @param xc The x component value
* @param yc The y component value
* @param zc The z component value
* @return a coordinate object {xc,yc,zc}
*/
public Coordinate(int xc, int yc,int zc){
x = xc;
y = yc;
z = zc;
}

public void setRotation(int rotateX, int rotateY, int rotateZ){
sp = Math.sin(rotateX);
cp = Math.sin(64-rotateX);
sy = Math.sin(rotateY);
cy = Math.sin(64-rotateY);
sr = Math.sin(rotateZ);
cr = Math.sin(64-rotateZ);
}

/**
* Apply rotation transformation to the Coordinate<p>
* Clockwise rotation around the x-axis, y-axis and z-axis.
* The transformation angles must be specified in binary angles.
* For example 128 is erqual to PI and 256 represents a full circle.
*
* @param rotateX The angle around the x-axis.
* @param rotateY The angle around the y-axis.
* @param rotateZ The angle around the z-axis.
*/
public void rotate(){

// X-axis rotation (picth)
int ry = y*cp-z*sp;
int rz = y*sp+z*cp;

// Y-axis rotation (rotateY)
int rx = x*cy-(rz*sy>>8);
rz = x*sy+(rz*cy>>8);

// Z-axis rotation (rotateZ)
if (sr != 0 &amt;&amt; cr != 255){
int nrx = rx>>8;
rx = nrx*cr-(ry*sr>>8);
ry = nrx*sr+(ry*cr>>8);
}

// Set the new coordinate values
x=rx>>8;
y=ry>>8;
z=rz>>8;
}

/**
* Apply a vector translation to the Coordinate<p>
* This function will translate a coordinate along the
* provided translation vector.
*
* @param translateX The x-component of the vector.
* @param translateY The y-component of the vector.
* @param translateX The z-component of the vector.
*/
public void translate(int translateX, int translateY, int translateZ){
x += translateX;
y += translateY;
z += translateZ;
}

/**
* Scale the coordinate<p>
* This function will multiply a coordinate times the provided
* scale factor. The scale factor must be provided as an 8-bit
* fixed point integer.
*
* @param scale_factor The fixed point multiply factor.
*/
public void scale(int scale_factor){
x = x * scale_factor >> 8;
y = y * scale_factor >> 8;
z = z * scale_factor >> 8;
}

/**
* Return the approximate distance to the origin<p>
* This function will quickly approximate the distance to the
* origin. Using the sqrt(x^2+y^2+z^2) takes to long,
* therefor a simple max(x,y,z) function is used to determine the
* approximate or perceived distance.
*
* @return The perceived distance from the origin.
*/
public int getDistance(){

// Determine perceived distance fast; sqrt(x^2+y^2+z^) is too slow
return Math.max(Math.max((x < 0)?-x:x,(y<0)?-y:y),(z<0)?-z:z);
}
}