Laser effect
by Darren Stuart · in Torque Game Builder · 04/08/2005 (3:30 pm) · 38 replies
I want to create a decent looking laser beam.
I am going to attach an effect to a sprite and cover it.
but i can't seem to create a desent looking laser burst. I want it to look blue.
any ideas how to create one?
Cheers Guys
I am going to attach an effect to a sprite and cover it.
but i can't seem to create a desent looking laser burst. I want it to look blue.
any ideas how to create one?
Cheers Guys
#22
04/23/2005 (8:31 am)
That works really well. I like it.
#23
04/23/2005 (1:50 pm)
Ah, now that's more like it Darren! Nice to see this working in T2D!:)
#24
[20, 100, 200, 255, 255, 200, 100, 20]
That and I added a small 8x8 png sprite done in a similar style(only a circle instead) to get rid of that hard edge at the end of the beam.
It did get me thinking though... Could possibly use the technique to make a pretty cool looking lightcycles clone.
04/23/2005 (3:03 pm)
You did the hard work, Hans, all I did was create a simple 1x8 png file with the alpha set like this:[20, 100, 200, 255, 255, 200, 100, 20]
That and I added a small 8x8 png sprite done in a similar style(only a circle instead) to get rid of that hard edge at the end of the beam.
It did get me thinking though... Could possibly use the technique to make a pretty cool looking lightcycles clone.
#25
Also you will notice I am using a plain white pixel (with faded edges) and using setBlendColour to colorise to any color I like.
Somewhere, define this datablock:
and use this pixel or create your own for different effects.
And here is a pretty scribble:

to make that scribble I used the following code:
I am kludging the ability to use multiple lines by adding objects to an array. Tracking these lines would be difficult and involve a lot of sorting methinks, so that if I wanted to remove a certain line it could be quite difficult. How about creating lines by name rather than number.
So many possibilities, so little time! Many thanks to everyone here, we're one step closer to a solution :) Especially Hans!
I'm working on a lightening algorithm BTW.
04/28/2005 (8:51 am)
I've been thinking about this alot lately, there needs to be a way to manage multiple lines. So far, this is what I've come up with, which is actually mainly a hack of Han's code above, modified to allow absolute rather than object-relative positions.Also you will notice I am using a plain white pixel (with faded edges) and using setBlendColour to colorise to any color I like.
function draw_line(%where, %x1, %y1, %x2, %y2, %r, %g, %b, %a)
{
$line[$line_number] = new fxStaticSprite2D() {scenegraph = %where;};
$line[$line_number].setBlendColour(%r SPC %g SPC %b SPC %a);
$line[$line_number].setImageMap(PixelImageMap);
%difference = vectorSub2D(%x2 SPC %y2, %x1 SPC y1);
%sum = vectorAdd2D(%x2 SPC %y2, %x1 SPC %y1);
%midpoint = vectorScale2D(%sum, 0.5);
%angle = mRadToDeg(mAtan(getWord(%difference, 0), getWord(%difference, 1)));
$line[$line_number].setPosition(%midpoint);
$line[$line_number].setSize("0.5" SPC vectorLength2D(%difference));
$line[$line_number].setRotation(-%angle);
$line_number++;
}Somewhere, define this datablock:
// --------------------------------------------------------------------
// Pixel ImageMap.
// --------------------------------------------------------------------
datablock fxImageMapDatablock2D(PixelImageMap)
{
mode = full;
textureName = "~/client/images/pixel"; //or your custom pixel image
};and use this pixel or create your own for different effects.
And here is a pretty scribble:

to make that scribble I used the following code:
function do_diddy()
{
draw_line(t2dSceneGraph, getRandom()*50 - getRandom() * 50, getRandom()*50 - getRandom() * 50, getRandom()*50 - getRandom() * 50, getRandom()*50 - getRandom() * 50, getRandom()*255,getRandom()*255,getRandom()*255,255);
schedule(50,0,"do_diddy");
}...Which draws lines randomly over the t2dSceneGraph.I am kludging the ability to use multiple lines by adding objects to an array. Tracking these lines would be difficult and involve a lot of sorting methinks, so that if I wanted to remove a certain line it could be quite difficult. How about creating lines by name rather than number.
So many possibilities, so little time! Many thanks to everyone here, we're one step closer to a solution :) Especially Hans!
I'm working on a lightening algorithm BTW.
#26
04/28/2005 (8:54 am)
Btw, with all this talk of lines, how about some routines for creating old-skool wireframe vector graphics... 3D? In T2D? Why not???
#27
wireframe vector graphics... hmm well you could always make 3D models, animate them, and display them as wireframe and get that to render out into sprites
04/28/2005 (8:19 pm)
Cool little function and great demo!wireframe vector graphics... hmm well you could always make 3D models, animate them, and display them as wireframe and get that to render out into sprites
#28
04/29/2005 (1:15 am)
When it comes to making 2D out of 3D, I always feel that we have a solid product in TGE. If we want some sort of 3D vector graphics, it could be as simple as a wireframe cube, then I'd just use TGE instead.
#29
I don't know if all this talk about lines constitutes a thread hijack or not, but at work today I was thinking even more about this as I filled up bags of grain...
I'm thinking it would be nice to be able to group lines into objects, like circles or squares, which are really just arangements of lines. It would be nice to attach this object to other objects, so you can make crosshairs that follow a target, or circles to represent planet orbits a-la Star Control 2 et al. Some kind of initialisation like this would be great:
new_line_object(parent_object, "a b c d e f ...", r, g, b, a, scenegraph, pixel_style)
Parent object is the object the line group will be attached to and follow.
"a b c d e f" is a string of arbitary length containing pairs of points defining lines.
r,g,b,a is the color of the line
scenegraph is where to draw it
pixel_style is the image datablock to use.
What do use think? I can make it happen, but is it worthwhile or is there a better way ?
04/29/2005 (3:24 am)
Yeh i know that, I was just kind of highlighting the amusing posibility of using a specificaly designed 2D engine in order to do 3D graphics, which with the current line routine would be fairly basic to impliment. Useless, but possible.I don't know if all this talk about lines constitutes a thread hijack or not, but at work today I was thinking even more about this as I filled up bags of grain...
I'm thinking it would be nice to be able to group lines into objects, like circles or squares, which are really just arangements of lines. It would be nice to attach this object to other objects, so you can make crosshairs that follow a target, or circles to represent planet orbits a-la Star Control 2 et al. Some kind of initialisation like this would be great:
new_line_object(parent_object, "a b c d e f ...", r, g, b, a, scenegraph, pixel_style)
Parent object is the object the line group will be attached to and follow.
"a b c d e f" is a string of arbitary length containing pairs of points defining lines.
r,g,b,a is the color of the line
scenegraph is where to draw it
pixel_style is the image datablock to use.
What do use think? I can make it happen, but is it worthwhile or is there a better way ?
#30
While I do agree that it would be nice to have different kinds of drawing primitives, I'd think the better solution would be to implement a new primitive in the engine, rather than using stretched and rotated sprites. Especially for curved lines.
There's definitly use for drawing primitives, because in some cases, using sprites just doesn't feel right.
04/29/2005 (4:38 am)
@AlexWhile I do agree that it would be nice to have different kinds of drawing primitives, I'd think the better solution would be to implement a new primitive in the engine, rather than using stretched and rotated sprites. Especially for curved lines.
There's definitly use for drawing primitives, because in some cases, using sprites just doesn't feel right.
#31
04/29/2005 (8:41 pm)
Hans, I agree that implimenting primitives in the engine will be much more efficient, this is all just a quick hack while we wait right? Anyway, I have enough info for my own evil schemes involving lines for the time being, thanks to all who helped.
#32
04/29/2005 (11:35 pm)
I need to figure out how to do a lightsaber :)
#33
04/30/2005 (3:34 am)
When I saw Alex's scribble screenshot I instantly thought it was a free-for-all battle between five hundred stealthed ninjas with tutti frutti lightsabers.
#34
04/30/2005 (9:04 am)
Randy, I think that can be done without even using the 'line code', just a simple sprite. Though it WOULD be usefull for making the saber grow/shrink, you could also simulate that by creating an animated sprite.
#35
04/30/2005 (10:03 pm)
Lol @hans
#36
I have tried playing around with it, but my knowledge of Torque Game Builder scripting is woefully inadequate
If the beam was always to be aligned horizontally, vertically or diagonally, I would probably start with a tile based sprite, but the beam will need to be shifted around a point at lots of different angles, so that won't work.
I've played around with the particle editor to generate a beam, but without much success. The only way I can get a beam is to have the number of particles very high, and I can't seem to accurately predict where the beam will end, only where it starts.
Any thoughts from seasoned TGB'ers on how they would go about generating a sustained laser beam effect?
Thanks,
Jonathan
08/12/2008 (6:19 am)
Well, this is an old thread, I know, but I am trying to generate a "laser beam" between two points, as described above. I suspect that the code-base has changed significantly in the last three years, and the code examples above don't seem to work.I have tried playing around with it, but my knowledge of Torque Game Builder scripting is woefully inadequate
If the beam was always to be aligned horizontally, vertically or diagonally, I would probably start with a tile based sprite, but the beam will need to be shifted around a point at lots of different angles, so that won't work.
I've played around with the particle editor to generate a beam, but without much success. The only way I can get a beam is to have the number of particles very high, and I can't seem to accurately predict where the beam will end, only where it starts.
Any thoughts from seasoned TGB'ers on how they would go about generating a sustained laser beam effect?
Thanks,
Jonathan
#37
To get a good laser effect I'd use this code, but on a tilemap with a scrolling image so that the beam scrolls from the shooter to the target very quickly. I'd also have some particles flying along the laser and particles bouncing off the target. Working out what your laser has hit, and how long it should be is another issue though!
08/27/2008 (12:20 am)
Jonathan - Han's code above should work - you just need to update some of the functions to their newer names:function beam(%shooter, %target) {
if (!isObject($beam))
$beam = new t2dStaticSprite() {scenegraph = t2dSceneGraph;};
$beam.setImageMap(redPixelImageMap);
%difference = t2dVectorSub(%target.getPosition(), %shooter.getPosition());
%sum = t2dVectorAdd(%target.getPosition(), %shooter.getPosition());
%midpoint = t2dVectorScale(%sum, 0.5);
%angle = mRadToDeg(mAtan(getWord(%difference, 0), getWord(%difference, 1)));
$beam.setPosition(%midpoint);
$beam.setSize("0.5" SPC t2dVectorLength(%difference));
$beam.setRotation(-%angle);
}To get a good laser effect I'd use this code, but on a tilemap with a scrolling image so that the beam scrolls from the shooter to the target very quickly. I'd also have some particles flying along the laser and particles bouncing off the target. Working out what your laser has hit, and how long it should be is another issue though!
#38
06/07/2010 (12:35 pm)
Thanks for the updated code, this came is very useful!
Torque Owner Joshua Spencer