Canvas 3d JS Library

where 3D is born!
  • rss
  • What is C3DL?
  • Download
  • Tutorials
    • Tutorial #1: Installing Canvas 3D Addon
    • Tutorial #2: A Scene and a Cube
    • Tutorial #3: Update Callback
    • Tutorial #4: Models 101
  • Development News
  • Demos
    • Typing Game V2.1
    • Typing Game V3 (0.3 Release)
    • Explorer
    • Flickr - Picking
    • Ricochet
    • FSOSS Pictures
    • Puzzler
  • Resources
  • Contact
  • About

New Road Model Added to 3D Map

Chris Bishop | 5 September, 2008 | 14:19
Road Model Added As I mentioned from a previous post I am working on getting the road models that Cathy made added to my demo.  I cleaned up and organized my code and now I have a cleaner and leaner demo with one road model being used. The other road model is a corner so I have to figure out how to add the corner pieces before each road segment.  It won’t be hard.  I just have to sit down and do it.  The code I was able to clean up was my road construction code. Road Object Explained I’ll break down how I create a road segment. When a road segment is made it is passed an argument list.  One of the arguments is the endPoint of the road segment in the scene.  The start segment is always 0,0,0.  I move the road segments later. The road object starts off as a Model with no scale at position 0,0,0.  I have to scale the object and then rotate the object to match the endPoint in the arguments list. Purpose to Show Code I’m not sure my code is the best way to determine where a line is pointing in 2D space.  I ignore the Y Axis in my demo.  I have a lot of if statements and thought maybe some one could point out an easier way.  Any ideas might become part of our library to help web developers work with 2d and 3d objects. Road Object Code Here is my code for rotating the road object.  It is long.  I check to see which 90 Degree quadrant of the 360 degree area around the -Z axis the road segment is pointing.  Using the start and end positions I create a triangle. Using the triangle I get its angle at the start point. I then determine which direction it is facing by comparing the end and start points x and z values.  Once I know which way it is facing I determine the number of radians needed to rotate from -Z Axis to have the actual road model point at the end point it was given. At most 5 if statements are hit.  If anyone has any ideas that would be great.
  // set the opposite and adjcent lengths
var opp = Math.abs( _endVec[ 2 ] );
var adj = Math.abs( _endVec[ 0 ] );
  // holds the new position
var pos;

  // the angle in radians from the -Z axis
_radians = Math.asin( opp / _length );

  // check if the end point is extending into the -Z axis
if ( _startVec[ 2 ] > _endVec[ 2 ] )
{
    // is the X of the startpoint is larger than the endpoint
  if ( _startVec[ 0 ] > _endVec[ 0 ] )
  {
      // set the radians from the -Z axis
    _radians = Math.PI / 2 - _radians;
  }
  else if ( _startVec[ 0 ] < _endVec[ 0 ] )
  {
      // rotate to the right of the -Z axis
    _radians = ( ( Math.PI / 2 ) * -1 ) - _radians;
  }
  else
  {
      // do not rotate
    _radians = 0;
  }
}
else if ( _startVec[ 2 ] < _endVec[ 2 ] )
{  // rotate to the left plus 90 degrees from the -Z axis
  _radians = Math.PI / 2 + _radians;    

    // is the X of the startpoint is larger than the endpoint
  if ( _startVec[ 0 ] > _endVec[ 0 ] )
  {
      // there is other code here
  }
  else if ( _startVec[ 0 ] < _endVec[ 0 ] )
  {
      // rotate to the left of the Z axis
    _radians *= -1;
  }
  else
  {
    _radians = 0;
  }
}
else if ( _startVec[ 2 ] == _endVec[ 2 ] )
{
    var len = _length / 2;
    if ( _startVec[ 0 ] > _endVec[ 0 ] )
    {
         // rotate to X with zero Z
       _radians = Math.PI/2;
    }
    else
    {
        // rotate to X with zero Z
      _radians = Math.PI/2;
    }
}
Categories
c3dl development
Comments rss
Comments rss

« 3d Google Map Progress c3dl text API »

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Search

Demos

  • Explorer
  • Flickr - Picking
  • FSOSS Pictures
  • Puzzler
  • Ricochet
  • Typing Game V2.1
  • Typing Game V3 (0.3 Release)

C3DL Development News

Portable Canvas v0.2!

Portable Canvas version 0.2 is available!!

It is now a usable browser, but restricted to pages on c3dl.org only. It can display canvas elements right out of the box, and doesn’t require anything else to be on your system (not even FF3!)*. It’s only been tested on Windows XP, but in theory it should be easy [...]

Namespaces and const

As our library expands with more functions, classes and global variables, the need for namespacing increases.  I started placing code in a C3DL namespace when I wrote the matrix stack operations.  However, yesterday I was looking in the constants.js file and saw the ‘tolerance’ variable.  It’s a const variable used when comparing floats to check [...]

Tutorials

  • Tutorial #1: Installing Canvas 3D Addon
  • Tutorial #2: A Scene and a Cube
  • Tutorial #3: Update Callback
  • Tutorial #4: Models 101

Archives

C3DL Development News

Recent Comments

  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • Portable Canvas v0.2!
  • Namespaces and const
  • DAE Scenegraph
  • The Matrix Stack
  • .obj to .dae
  • 0.5 Release and Other News!
  • Tracemonkey performance
  • More memory usage improvements
  • Canvas3D crashes in tracemonkey
  • Patched one hole in floating text
  • For name spaces, my... - Jeremy Giberson
  • Great! Really really... - Edson Mattos
  • Beautiful!... - Funtomas
  • Was no need to conta... - Andrew Smith
  • Andrew, have you con... - Funtomas
  • Thanks for posting t... - Andrew Smith
  • Vlad was in town and... - Cathy Leung
  • the upside down issu... - Cathy Leung
  • Got it! the addon is... - Bill Mill
  • Yes it does. The ad... - Cathy Leung



Canvas 3d JS Library

©2007- 2008 Canvas 3d JS Library

Disclaimer: This website is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.
The Canvas 3d JS Library and Demos found on this website are licenced under the MIT License

Creative Commons License