Java-Gaming.org
Java4K - to go         Javadoc:
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, peek at the official java tutorials or join us at irc #jgo.
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Coordinate system from a single vector  (Read 599 times)
0 Members and 1 Guest are viewing this topic.
Offline krasse

Sr. Member
**

Posts: 259
Medals: 13



« on: 2010-04-16 08:48:40 »

This is a member function of my Vec3D class that I often find very useful.
It constructs a coordinate system from a single 3D vector.
The code is adapted from the book "Physically Based Rendering".

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
        // v1, v2 and v3 are result vectors
  // v1 becomes parallel to this (a Vec3D)
  public void getCoordinateSystemFromThis(Vec3D v1, Vec3D v2, Vec3D v3) {
      Vec3D v = normalizeCopy();
      if (Math.abs(v.x) > Math.abs(v.y)) {
         double invLen = 1.0 / Math.sqrt(v.x * v.x + v.z * v.z);
         v2.set(-v.z * invLen, 0.0, v.x * invLen);
      } else {
         double invLen = 1.0 / Math.sqrt(v.y * v.y + v.z * v.z);
         v2.set(0.0, v.z * invLen, -v.y * invLen);
      }
      v3.set(v.crossProductCopy(v2));
      v1.set(v);
   }

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.189 seconds with 21 queries.