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;
}
} 