chiken invader beam

BlitzMax Forums/BlitzMax Programming/chiken invader beam

hub(Posted 2009) [#1]


hi !
i need some ideas to code a laser beam like this one ! Animstrip or just plain code ? Hope it's easy to code with bmax !


MGE(Posted 2009) [#2]
Very easy. Anim strips with a few frames of animation. Calculate the center point between start and destination, rotate, scale, wala...instant lightning bolt.

Granted you could also use sprite "segments" that look light jointed sections of the lightning bolt, but that takes more sprites, more code, etc. I would just use the anim strip rotated, scaled, etc.

Using the xscale, yscale parameters you could also mirror the bolts in real time too for diversity. ;)


GfK(Posted 2009) [#3]
Just done this myself. I have two points - a start, and an end. I also have some lightning images which are loaded as Pixmaps.

After I determine the distance between point A and point B, I use LoadImage/PixmapWindow to grab three random sections of pixmap of the right size.

To draw them, I use ATan2 to figure out the correct angle between the two points, then simply draw the image (one of the three grabbed images, picked at random). Finally I use a lightblended soft 'glow' effect to hide the joints, where needed.

The result is this:

(BTW: That pic is scaled to 200% of its original size, but I can't remember why)


Ked(Posted 2009) [#4]
(BTW: That pic is scaled to 200% of its original size, but I can't remember why)

I think you want it to be kept a surprise until your project is released.
here's a cryptic image from one of the nine minigames/bonus levels

That is a pure guess though, because I have no idea what you mean by a "cryptic image."


GfK(Posted 2009) [#5]
I think you want it to be kept a surprise until your project is released.
Ah yeah, that's right. I just found the pic on my webspace and forgot why it was there. :)


hub(Posted 2009) [#6]
Thanks !

@gfk : So you don't scale the image. Very interesting this pixmapwindow function. Into my game i use a lot of 'images' against pixmaps. As i consider pixmaps operations and display are slow...

could you explain more how you did your lighting image ? (size, graphics software used, ...).

Perhaps you could post some samples or pseudo code to help me to start with this : 'I use LoadImage/PixmapWindow to grab three random sections of pixmap of the right size' ? i've always some difficulties to correctly use pixmap functions !


GfK(Posted 2009) [#7]
As i consider pixmaps operations and display are slow...
They are slow, but they're there to be used. You can get away with sensibly sized pixmap operations on the fly, provided you don't overdo it, and only use pixmap manipulation where its absolutely necessary.

This tutorial should get you going in the right direction: http://www.photoshopcafe.com/tutorials/lightning/lightning.htm

In the end, I drew mine by hand. Basically a zig-zag line with an outer glow effect in PS, and a couple of thinner, feint zig-zag lines behind it.

I have a pool of three 800x32 pixmaps containing lightning. Could use less, or more, depending on your needs. I wanted my lightning to look very random and non-repetitive.

What you will basically do is keep these pixmaps in memory all the time, as PixmapWindow only returns a reference to the existing pixmap - it doesn't copy it.

Code:
lightning:TPixmap = LoadPixmap("800x32lightning.png")

...then...
myImage:TImage = LoadImage(PixmapWindow(lightning,x,0,width,32))

...and finally...
SetImageHandle myImage,0,16 'put the image handle at the left edge of the resulting image, and half way down


That last step is very important. Once you know the start and end points of the lightning bolt, and have determined the angle between the two points with ATan2(), all you need to do is use SetRotation with that angle, and draw the image at the start point. It will automatically align to point towards the end point.

A final note - you *could* use a fixed lightning image and stretch/squash it. But I've seen it done this way before and it generally looks very poor with longer or shorter lightning bolts that are too far away from the bolt's original size. I'd only use that method if I was very concerned about video RAM usage and wasn't bothered about it looking a bit rubbish.


hub(Posted 2009) [#8]
Many thanks gfk for these explanations !


hub(Posted 2009) [#9]
Here my first try :

Download the image file here


Strict

Global start_x, width
Global x1, y1, x2, y2:Float

Global lightning:TPixmap = LoadPixmap("test_ligne1.png")


Graphics 800, 600


x1 = GraphicsWidth() / 2
y1 = GraphicsHeight() / 2


While Not KeyDown (KEY_ESCAPE)

	x2 = MouseX()
	y2 = MouseY()
	  
	Cls
	SetColor 255, 0, 0
	DrawOval x1 - 3, y1 - 3, 6, 6
	DrawOval x2 - 3, y2 - 3, 6, 6
	SetColor 255, 255, 255

	If MouseDown(1) Then
	
			Local myImage:TImage 
		 	Local Angle
			
			Angle = ATan2 ((y2 - y1), (x2 - x1))
			width = Sqr ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
			If width > PixmapWidth(lightning) Then
				width = PixmapWidth(lightning)
			End If
			start_x = Rand (0, PixmapWidth (lightning) - width)
			myImage = LoadImage(PixmapWindow(lightning, start_x, 0, width, 32))
		 	SetImageHandle myImage, 0, 16
			
			SetRotation Angle
			SetBlend ALPHABLEND
			DrawImage myImage, x1, y1
			SetBlend LIGHTBLEND
			DrawImage myImage, x1, y1
			SetRotation 0.0
			myImage = Null
	Else
	
		DrawLine x1, y1, x2, y2
		
	End If
		
	Flip

Wend



GfK(Posted 2009) [#10]
Easy when you know how. :)

A note of caution though - you'll need to ensure that the size of the image you're trying to grab from the pixmap, doesn't exceed the size of the pixmap itself. With the code above, move X1,Y1 to 0,0, and click on the bottom-right corner of the screen - you'll get a pixmap out of bounds error.


TommyH(Posted 2009) [#11]
Thanks for that great explanation and sample, GfK and hub!

I always wondered how to do that in a simple way.

Should be added to the code archive ;-)


hub(Posted 2009) [#12]
i've updated the sample code and produce a new gfx file more interesting to show the effect.


hub(Posted 2009) [#13]
note that it should be a good thing to find a way to replace the sqr call inside the previous code (sqr function is slow)


therevills(Posted 2009) [#14]
Thanks for the code example hub :)

You could use a look up table to speed up sqr:




hub(Posted 2009) [#15]
Good idea therevills !