Jump to content
  • Advertisement
Sign in to follow this  
mental.breakdance

standing cross section verts on an arbitrary surface

This topic is 2341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

Currently just confusing myself by trying to align things in 3D. I've discovered that describing my end goal is almost as hard as solving it, so here goes..

If I have an array of Vector2s that define the cross section of something I want to extrude along a path, how can I align that cross section to the normal of a surface? I.e. how do I work out the Vector3s that define the cross section as though it were 'coming out' of the surface? Assuming I give it a direction along the surface by which to point in..

Below is my code so far (which is blatantly wrong) - I'm wondering if I'm close to going about it the right way...



Vector3[] GetPoints( Vector3 surfaceNormal, Vector3 pointOnSurface, Vector3 directionAlongSurface, Vector2[] crossSectionVerts, Vector2 crossSectionOrigin )
{
List<Vector3> vertices = new List<Vector3>();
Quaternion startRotation = Quaternion.identity;
Vector3 startDirection = Vector3.forward;
if( surfaceNormal != Vector3.up )
{
float startAngle = Vector3.Angle( Vector3.forward, surfaceNormal );
Vector3 startCross = ( surfaceNormal == Vector3.back ) ? Vector3.up : Vector3.Cross( Vector3.forward, surfaceNormal );
startRotation = Quaternion.AngleAxis( startAngle, startCross );
startDirection = startRotation * startDirection;
}

float angle = Vector3.Angle( startDirection, directionAlongSurface );
Vector3 cross = ( directionAlongSurface == -startDirection ) ? surfaceNormal : Vector3.Cross( startDirection, directionAlongSurface );
Quaternion rotation = Quaternion.AngleAxis( angle, surfaceNormal );

for( int i = 0; i < crossSectionVerts.Length; ++i )
{
vertices.Add( (rotation * startRotation * (crossSectionOrigin + crossSectionVerts)) + pointOnSurface );
}
return vertices.ToArray();
}



Cheers

Share this post


Link to post
Share on other sites
Advertisement
never mind.. sussed it. Was using Quaternions when I blatantly didn't need to. Ignoring the origin of the cross section (will add back in next) the following works:



Vector3 cross = Vector3.Cross( surfaceNormal, directionAlongSurface );

for( int i = 0; i < numCrossSectionVerts; ++i )
{
vertices[currentVertIdx++] = ( ( crossSectionVerts.x ) * cross ) + ( crossSectionVerts.y * surfaceNormal ) + points[0];
}


phew that was a lot easier than I made it out to be =/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!