Jump to content
• Advertisement

# standing cross section verts on an arbitrary surface

This topic is 2464 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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5
• Advertisement

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013736
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!