Page 1 of 1

Getting a vector3df halfway between 2 vector3df's

Posted: Wed Aug 02, 2006 2:59 am
by Browndog
Can someone give me an example of how I can get a point halfway between 2 points.

Thanks

Posted: Wed Aug 02, 2006 3:13 am
by vitek
There are a few ways. Assuming you have the two points as vectors...

Code: Select all

core::vector3df p1(0, 0, 0);
core::vector3df p2(10, 10, 10);

// use linear interpolation to find halfway point
core::vector3df p3 = p1.getInterpolated(p2, .5f);

// add half of the distance between the two points
// to the first point. i.e. get vector from p1 to p2,
// cut it in half, and then add that to p1.
core::vector3df p4 = p1 + ((p2 - p1) * .5f);

Posted: Wed Aug 02, 2006 3:19 am
by Browndog
ok thanks, works well :)

Posted: Wed Aug 02, 2006 3:46 am
by jam
vitek wrote:

Code: Select all

core::vector3df p1(0, 0, 0);
core::vector3df p2(10, 10, 10);
// add half of the distance between the two points
// to the first point. i.e. get vector from p1 to p2,
// cut it in half, and then add that to p1.
core::vector3df p4 = p1 + ((p2 - p1) * .5f);
Just out of curiosity, why not do it like this?

Code: Select all

core::vector3df p4 = ((p2+p1) * .5f);

Posted: Wed Aug 02, 2006 3:57 am
by vitek
Yup. Like I said, there are several ways to do it. I just showed the two that were easiest to explain.

Posted: Wed Aug 02, 2006 4:01 am
by jam
Fair enough :)

Posted: Wed Aug 02, 2006 5:18 am
by Browndog
yeah its always so simple when you know how to do it. No matter how much I learn about this stuff it always seems like I am just scratching the surface of what there is to know.

Posted: Wed Aug 02, 2006 10:21 am
by hybrid
I think the nicest (and equally fast) method is

Code: Select all

core::vetor3df center = core::line3df(start,end).getMiddle();

Posted: Wed Aug 02, 2006 4:17 pm
by vitek
Now there's one I didn't think of :)