contrat a line

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
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

contrat a line

Post by Seraph »

i have a line with the start and the end point. I need to reduce the length of 40px. How? Thanks
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

I think you need to be more specific.

However, from what I am getting you have a line and you want to only draw it up to 40px long.

This should do that: (I am assuming 2D since you said pixels)

Code: Select all

irr::core::vector2di a(2,100);
irr::core::vector2di b(100,300);
irr::f32 dir = atan2f(b.Y-a.Y,b.X-a.X);//Get the direction between the two points
irr::core::vector2di bfinal(cosf(dir)*40,sinf(dir)*40);//Get where b would be (in relation to 0,0) 40px in direction dir
bfinal+=a;//Translate it to a to it puts it in the right place
//Just a test drawing thing to show it working:
irrDevice->getVideoDriver()->draw2DLine(a,b);
irrDevice->getVideoDriver()->draw2DLine(a,bfinal,video::SColor(255,255,0,0));
I don't know if this is the best way to do it, but it should work!

~DtD
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

:D thanks for the answer

the problem is this:

i have two points: vector3df(x1,y1,z1) and vector3df(x2,y2,z2)
for example the line that connects these two points is long 200px. Now i need to find the end point of the line that starts in vector3df(x1,y1,z1), point to vector3df(x2,y2,z2) and is long 160px.

Thanks for the help
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

vec1 (start)
vec2 (end)
vec3 (from vec1 to vec2)

vec3 = vec2 - vec1

Since you know the length of vec3 (200 units) you do this:
vec3.normalize() * 160

So its length is 160 and finally:
vec3 += vec1

Now vec3 should be what you want.

If you don't know the length of vec3 you can easily calculate it:
|vec3| = squareroot( pow(x,2) + pow(y,2) + pow(z,2) )

However, you should also check the Irrlicht API since there are lots of functions already built in. For instance:

getDistanceFrom
normalize
getLength
setLength
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

And, for the record, 3D points are not pixels. They are "3D Units" (an exact unit depends on your engine, etc. Usually with a physics engine, 1 unit = 1 meter)

~DtD
Post Reply