Draw line in correct direction

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Draw line in correct direction

Post by QProgrammer »

Hello

I have a problem and i hope someone can help me out.

I have a cylinder in my scene, this cylinder can rotate around itself (over Y axis)

I want to draw 3 direct horizontal lines from center of this cylinder according to its rotation.
Each line make a 30 degree angle to previous one.(see the screen-shot to get the idea)

Assuming that length of each line is 500,
here is what i tried so far :

Code: Select all

 
For line 1:
 
Start point =   Node->GetPosition();
End point =    Node->getRotation().rotationToDirection()*500;
 
For next lines, 
 
Direction = Node->getRotation();
Direction.rotateXZBy(30);
Direction = Direction.rotationToDirection();
Start point = Node->GetPosition();
End point =  Node->getPosition()+ (Direction*500)

Image

It seems it works (when rotation is zero) , but it doesnt :( when cylender rotates, all three lines will be messy and in wrong directions.

I know there should be an easy solution for this that i am not aware of. Im sure my approach is wrong and maybe silly.
Please help me understand my mistake and learn the correct way.

Kind regards
ben
Last edited by QProgrammer on Tue Jun 17, 2014 1:11 pm, edited 1 time in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Draw line in correct direction

Post by hendu »

getRotation does not give you a direction vector. It gives you euler angles, which are obviously wrong to pass to rotate*By.
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Re: Draw line in correct direction

Post by QProgrammer »

Any suggestion how to calculate three lines directions?
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Draw line in correct direction

Post by thanhle »

Hi,
Isn't your next line end point is below?

End Point = Direction*500
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Re: Draw line in correct direction

Post by QProgrammer »

thanhle wrote:Hi,
Isn't your next line end point is below?

End Point = Direction*500

yea, that was a typo, actually its like this :

End point = Node->getPosition()+ (Direction*500)


Actually the middle line (without angle) is working fine , im not sure how to apply an angle to a direction vector, problem could be that
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Draw line in correct direction

Post by hybrid »

I guess your problem is the drawing. Please show the part where you draw the lines and make sure that you set up the proper transformation matrix before that.
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Re: Draw line in correct direction

Post by QProgrammer »

hybrid wrote:I guess your problem is the drawing. Please show the part where you draw the lines and make sure that you set up the proper transformation matrix before that.
Thank you for your answer. So you say my approach is correct? i mean Direction.rotateXZBy(30); really does what i wanted?

I dont think drawing is wrong, it looks like this:

Code: Select all

matrix4 id;
id.makeIdentity();
this->vdriver->setTransform(video::ETS_WORLD, id);
 
Driver->draw3DLine(StartPoint, EndPoint, SColor(255,0,0,255));
Driver->draw3DLine(StartPoint2, EndPoint2, SColor(255,0,216,255));
Driver->draw3DLine(StartPoint3, EndPoint3, SColor(255,222,0,255));

Anyway, i solved my problem using sin(degToRad(30+Rotation.Y))*500) and cos(degToRad(30+Rotation.Y))*500) to draw other lines with 30 degree angle
(however i would like to learn the correct vector math to achive that)

-----------------------------------------------------------------
I have a similar question if you can help please,
take a look at below picture, its same situation, i have rotating cylinder.

I have the blue line vector and start/end positions.
How can i draw two other lines (Red and Green) which are parallel with current line with a constant distance from it?

In general, if we have a 2d line, how can we calculate a parallel vector with a distance from it?
Image
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Draw line in correct direction

Post by thanhle »

1) Dot product of two vectors through a common point = 0, gives you the perpendicular line.
2) From there on you can calculate separation point based on the desired distance.
3) After that just use the direction of the original vector to find the end point of both the mirrowed lines.

Goodluck
Regards
Thanh
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Re: Draw line in correct direction

Post by QProgrammer »

thanhle wrote:1) Dot product of two vectors through a common point = 0, gives you the perpendicular line.
2) From there on you can calculate separation point based on the desired distance.
3) After that just use the direction of the original vector to find the end point of both the mirrowed lines.

Goodluck
Regards
Thanh
Hello thanhle, thank you so much for your help.

Actually i got it working. However I didn't understand how to find the perpendicular line using Dot Product?
Which vectors do i need to use in DotProduct? StartPoint and End point of Blueline? i couldn't figure it out, so i rotate blueline end point by 90 degree and got the perpendicular line, and do the rest as you said and it worked fine. I appreciate your help though, thank you.

Image
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Draw line in correct direction

Post by thanhle »

Yup! the blue line, which is the original vector.
Well done :).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Draw line in correct direction

Post by hybrid »

QProgrammer wrote:
hybrid wrote:I guess your problem is the drawing. Please show the part where you draw the lines and make sure that you set up the proper transformation matrix before that.
Thank you for your answer. So you say my approach is correct? i mean Direction.rotateXZBy(30); really does what i wanted?
Actually I didn't see you mentioning that first line always works, but only your initial comment that after rotation occurs it does not work at all. So I assumed that it's a messed up transformation upon drawing.
Transformation of vector shoudl work when converting to direction as with first line and than taking a rotation vector and using it for transforming the direction vector. Would be less code, but maybe more computations (as it would be more general). Maybe using the direction vector with rotateXZBy does also work then.
Post Reply