A simpler way to make a laser beam

terblus

  • Posts: 205
I've been trying to make a UFO passing over head fire a laser beam down onto the scene. The beam should originate underneath the saucer and move the same speed as it. The beam should be as many tiles long as the vertical distance between the saucer and the nearest solid tile directly underneath it. This is variable depending on how high or low the nearest tile is to the bottom of the scene and will change as the beam moves. Of course the beam should be able to interact with the player actor.

The only way I could think to do this was to have the UFO generate a single tile actor underneath it. This actor would then check if it was touching the ground, and if it wasn't, it would create another instance of itself directly underneath itself. In this way, there would appear to be a solid beam from the UFO to the ground, however near or far it was. This method seems really inelegant to me and I foresee it causing problems. Is there a better way to do this?

« Last Edit: August 01, 2011, 01:08:24 am by terblus »

coleislazy

  • *
  • Posts: 2607
You may be able to use the [grow actor] block in the actor->tweening category. I haven't tested this, but I would create the graphic for a laser that is only 1 pixel tall, but as wide as you intend for it to be. Then, you could pass the distance from the UFO to the ground into the block, essentially using it as the percentage to grow (multiplying it by 100). It should stretch the image to the correct height. Again, I must stress that I haven't actually tried this, its just where I would start. Good luck!

coleislazy

  • *
  • Posts: 2607
Ok, this sounded cool, so I tested it real quick. It does work, however growing it scales it in both y and -y directions at the same time, so you'd have to create the actor in the middle of the UFO and the ground.

terblus

  • Posts: 205
Thanks for the help. How do I get the distance between the UFO and the nearest block on its y-axis though?

And while this isn't so important, would there be a way to have the 1 pixel wide beam start at the UFO, and then slowly extend down to the ground? That might be a neat effect.

dubealex

  • Posts: 26
Well, I doN,t know how you do you level design, but a terribly easy way to make your beam "appear" the same height as the highest tile, is to use layers. Put or create the beam layer on the back, and make the tiles and actor above. That way, your beam will always appear as it stops when a tile is there. Of course, if you have level where there's tunnel and you seem between the floor, that's another issue.

Another way would be to create a "beam-bottom" actor, and make it slide down until it it a tile or anything you want it to stop, and at the same time, create a region the size you want starting from that actor, with a variable size... so if your tiles are 32x32, each time you beam-bottom actor goes down 1 tile, you increase and change the position of your region by 32 pixel too.

Then for animation, if you want a simple color thing for the beam, you can create a rectangle on that region. If you need the entire beam to be an actor, it might be more complex.

« Last Edit: August 01, 2011, 04:30:28 pm by dubealex »

Epic428

  • Posts: 1118
Just curious, and perhaps someone else more familiar could elaborate, but couldn't you use some sort of raycasting method?

Basically, the way I am envisioning it is that the ray would continue until it "collides" with a tile or actor, and then you draw a rectangle for the beam so that its height is equal to the length of the ray.

If that doesn't work or can't work, another option might be to use a version of Alexin's actor picker behavior that I have, that basically returns a list of actors along a line. I don't think it picks up tiles, and I'm not sure if that is something that can be added or not.

Ultimately though, the goal would be to get any actors/tiles along the line, and then check their Y values, whichever one has the closest Y value, draw the beam from that value to the ship.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Greg

  • *
  • Posts: 1259
There is raycasting, but I don't believe it works for tiles.  I think it just works for finding Actors along a line.  It would probably be a good idea for us to have a function that would return any type of object along a line (including tiles). 

terblus

  • Posts: 205
Thanks to everyone who took the time to reply.

dubealex: I do intend for there to be areas in which the beam will hit a tile that the actor can be safe underneath. I'm not sure if I need the entire beam to be an actor, but I imagine I would. If the player touches any part of the beam they should take damage.

coleislazy: In the version you made, did the beam just go to the bottom of the scene, or were you able to make it stop at the nearest tile on the y-axis?

coleislazy

  • *
  • Posts: 2607
coleislazy: In the version you made, did the beam just go to the bottom of the scene, or were you able to make it stop at the nearest tile on the y-axis?

I just tested to see if it could grow properly. I didn't do anything else.

Greg

  • *
  • Posts: 1259
I created a quick and dirty sample game -

http://www.stencyl.com/game/play/6028

Uploaded to Forge as UFO.  Might not be the most efficient - but it spawns a laser actor (not drawn) which then moves down until it hits a tile.  A line is drawn between the ship and the actor (can be any color, I chose red).  While firing, it's checking for any Actors in the laser beam region and then kills those actors if they're in the "enemies" group. 

Maybe this will help, may be not - but it's up there if you want to look at it. 

terblus

  • Posts: 205
Thanks for this Greg, I appreciate it. I've been trying to understand how what you've made works and I've been having some trouble. Where did the "Actor In Area" block come from and how does it work? Does it activate when there is any actor in a specific area? How does it know what area to look in? What is Target and how come it isn't in the palette?