# standing cross section verts on an arbitrary surface

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

## 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 on other sites
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 =/

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12