Jump to content
  • Advertisement
Sign in to follow this  

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

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



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
        // Back face
        // Left face
        // Right face
        // Top face
        // Bottom face

Image has glow but its a cube facing up





Thanks Again All.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!