• Create Account

# standing cross section verts on an arbitrary surface

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1mental.breakdance  Members   -  Reputation: 104

Like
0Likes
Like

Posted 20 March 2012 - 03:37 PM

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[i])) + pointOnSurface );
}
return vertices.ToArray();
}


Cheers

### #2mental.breakdance  Members   -  Reputation: 104

Like
0Likes
Like

Posted 20 March 2012 - 05:13 PM

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[i].x ) * cross ) + ( crossSectionVerts[i].y * surfaceNormal ) + points[0];
}


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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS