x


Generating a cube mesh with two given points

I'm attempting to create a script that takes two positions from the player, and creates a cube between those two points with a given height and depth. I can manage to generate a single face (Unity's Mesh library makes it easy to do), but I run into trouble when I try to create the other faces. Since the mesh is generated between two points, it has the appearance of being "rotated," but its forward vector remains relative to the world since the transform is never actually rotated. You can see an example of what I mean here:

alt text

This makes generating the other faces less than trivial (as far as I can tell) since I can't just use the first face's transform.forward to place the remaining vertices. If anyone has any experience with this let me know, and if you need any elaboration/code feel free to ask. Thanks!

more ▼

asked Aug 14 '11 at 02:37 PM

ElijahT gravatar image

ElijahT
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The Cross Product of two vectors is a vector pointing at right angles to both those vectors. Think about how that gives you exactly the vector you need: one pointing in the depth direction of your cube, the displacement to the 4 vertices of the back face of the cube, given each of the 4 front vertices.

(BTW, well done getting that far first - you've actually done the hard part!)

more ▼

answered Aug 14 '11 at 02:43 PM

Waz gravatar image

Waz
6.5k 23 33 71

Brilliant! I'm less than familiar with vector math and imagined there would be a simple way to do it. Thanks so much!

Aug 14 '11 at 02:50 PM ElijahT
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2247
x1403
x300
x146
x38

asked: Aug 14 '11 at 02:37 PM

Seen: 1446 times

Last Updated: Aug 14 '11 at 03:27 PM