Connecting moving objects with lines
by Jason "tekanoid" Hubbard · in Torque Game Builder · 11/12/2008 (9:04 pm) · 5 replies
Hey everyone. I've been playing around with this for a while, but I haven't come up with a surefire method yet.
I need to draw lines between objects moving on the screen. What I've done so far is to create a small square sprite that is the basis for my "line". To start with, I've chosen a location on the left side of the screen to act as the anchor point for the first line. Then I've applied the FaceMouse behavior to the square/line, and as the mouse cursor is moved around the screen, I calculate the distance from the anchor point to the cursor, move the square/line to the midpoint of that line and increase its width to the length of that distance. That way, as I move the mouse around the screen, there's always a line connecting it to the anchor point.
Now what I haven't been able to do is:
a) set an object other than the cursor as the right end of the line
b) set a location other than the original anchor point as the left end of the line (i.e. an object)
The objects that I need to connect with lines will be moving around the screen, so I would need to constantly recalculate the position and width of the square/line, but I'm having trouble updating the positions and angles fast enough to make it appear smooth.
Has anyone else done something like this before?
I need to draw lines between objects moving on the screen. What I've done so far is to create a small square sprite that is the basis for my "line". To start with, I've chosen a location on the left side of the screen to act as the anchor point for the first line. Then I've applied the FaceMouse behavior to the square/line, and as the mouse cursor is moved around the screen, I calculate the distance from the anchor point to the cursor, move the square/line to the midpoint of that line and increase its width to the length of that distance. That way, as I move the mouse around the screen, there's always a line connecting it to the anchor point.
Now what I haven't been able to do is:
a) set an object other than the cursor as the right end of the line
b) set a location other than the original anchor point as the left end of the line (i.e. an object)
The objects that I need to connect with lines will be moving around the screen, so I would need to constantly recalculate the position and width of the square/line, but I'm having trouble updating the positions and angles fast enough to make it appear smooth.
Has anyone else done something like this before?
#2
It's probably not something you'd want to have with each onMouseMove() call or onSceneUpdate(). You can probably get away with scheduling it every 50ms.
If you have to, you could re-write the function in C++ which might give you a little performance boost.
Otherwise, that is the approach I would have taken.
11/26/2008 (4:09 pm)
How often are you recalculating the position?It's probably not something you'd want to have with each onMouseMove() call or onSceneUpdate(). You can probably get away with scheduling it every 50ms.
If you have to, you could re-write the function in C++ which might give you a little performance boost.
Otherwise, that is the approach I would have taken.
#3
12/02/2008 (9:19 pm)
Hi Nikos. For the line attached to the mouse, I do recalculate the position on each onMouseMove, and it appears very smooth. It's just when I have a line connecting two (non-cursor) objects and I use onUpdate on the SceneGraph that it recalculates slowly.
#4
12/03/2008 (4:55 pm)
I would recommend using a schedule every 50ms, see if it moves more smoothly. I suspect it will.
#5
12/05/2008 (11:02 pm)
I will try that. Also, do you know if there's an easier way to create lines in TGB?
Torque Owner Jason "tekanoid" Hubbard