Sign in to follow this  
ankhd

How Would You Make A Quad Based On a Direction in 3D.

Recommended Posts

Hello.

 

I have a direction created from 2 3d locations, and a Size value. With this how could I create a quad or for points is what I would like that are

on the plane facing the direction at Size away from each other. the direction vector would be the centre.

 

So far I have.

 

float3 dir = start - target;

 

float3 perp = Perpendicular(dir);

 

float3 up = float3(0.0, perp.y, 0.0);//not sure if this is correct

 

now I'm a bit lost to what to do next to get my 4 points.

 

Do I do offset perp and up by +- Size like. perp * Size = x point.

 

Do I need to create a plane that faces the direction

 

Does dir and the length of dir = plane;

 

Share this post


Link to post
Share on other sites

Some pseudocode: 

dir = normalize(target - start)
up = (0, 1, 0)
halfSize = size / 2

// todo: handle case when (dir == up)
o1 = cross(dir, up) * halfSize
o2 = cross(o1, dir) * halfSize

p1 = start + o1 + o2
p2 = start + o1 - o2
p3 = start - o1 - o2
p4 = start - o1 + o2

Share this post


Link to post
Share on other sites

Nice work Iwm.

 

I just did a test with a normalize on the 2 crossproducts it works.

 

Thank you so much. I'm going to work on the up direction tomorrow oh today it is allready.

 

What exactly going on with that code...

 

Time for bed.

Share this post


Link to post
Share on other sites

Hi all.

 

My first extrude in a direction.

 

I have updated my code to acount for the up by using a perpendicular vector.

 

Thank you GameDev.

 

here is the updated code to create a 8 location of a cube  faceing any direction

//the face vertex points defines our cube
//float3 p1, p2, p3, p4, p5, p6, p7, p8;
float3 vetexdata[8];


//we need to get the direction we are heading
float3 dir = normalize(target - pos);

//create a up vector
float3 up = Perpendicular(dir);//float3(0.0, 1.0, 0.0);

float halfsize = 200;//wdthp * 0.5;

float extrudelength = length(target - pos);

// todo: handle case when (dir == up)
float3 o1 = normalize(cross(dir, up)) * halfsize;
float3 o2 = normalize(cross(o1, dir)) * halfsize;

//this is the first face of the cube starting at our position facing the direction we want to extrude in
vetexdata[0] = pos + o1 + o2;
vetexdata[1] = pos + o1 - o2;
vetexdata[2] = pos - o1 - o2;
vetexdata[3] = pos - o1 + o2;

//the second face at the target location
vetexdata[4] = vetexdata[0] + dir* extrudelength;
vetexdata[5] = vetexdata[1] + dir* extrudelength;
vetexdata[6] = vetexdata[2] + dir* extrudelength;
vetexdata[7] = vetexdata[3] + dir* extrudelength;


//the above defines our cubes face vertices now we need to lay them out
//to form eighter the 4 faces with no end caps or the whole cube with end caps

//the easyest way I think is to make a index list with 3 index for a triangle
/*
uint indices[37]
{
        // Front face
        0,1,2,
        0,2,3,
        // Back face
        4,6,5,
        4,7,6,
        // Left face
        4,5,1,
        4,1,0,
        // Right face
        3,2,6,
        3,6,7,
        // Top face
        1,5,6,
        1,6,2,
        // Bottom face
        4,0,3,
        4,3,7,
};
*/

Image has glow but its a cube facing up

 

First3DExtrude.jpg?psid=1

 

 

Thanks Again All.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this