Getting a vector3df halfway between 2 vector3df's

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Getting a vector3df halfway between 2 vector3df's

Post by Browndog »

Can someone give me an example of how I can get a point halfway between 2 points.

Thanks
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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);
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

ok thanks, works well :)
jam
Posts: 409
Joined: Fri Nov 04, 2005 3:52 am

Post 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);
system-independent, adj.:
Works equally poorly on all systems.
-- unknown
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Yup. Like I said, there are several ways to do it. I just showed the two that were easiest to explain.
jam
Posts: 409
Joined: Fri Nov 04, 2005 3:52 am

Post by jam »

Fair enough :)
system-independent, adj.:
Works equally poorly on all systems.
-- unknown
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post 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.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I think the nicest (and equally fast) method is

Code: Select all

core::vetor3df center = core::line3df(start,end).getMiddle();
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Now there's one I didn't think of :)
Post Reply