Is there a better way to draw a laser? [SOVLED]

Omniso

• Posts: 192
I recently thought of making an enemy that fires an extending laser in front of it. I wanted the laser to start as a line from the center of the enemy, and extend forward from that center at any given angle until it reaches the wall. Immediately, Sine and Cosine seemed like the perfect functions to use for this given situation, and I decided to try using trigonometry to accurately push the  endpoint of the line based on the facing angle of the enemy.

I tried applying this by creating a dummy example as shown below.

This works, but then caused my game's frame rate to drop into the abyss as several lines began to travel fairly far. Stencylpedia said to avoid trigonometry because it's "expensive", so is there any other way to do this? Trigonometry is the first thing that occurs to me when sketching out any problem that involves angles.

Edit: One of the things I want to be able to do with this is allow each enemy to slowly turn their individual lasers as shown below with the dummy actors

« Last Edit: March 18, 2020, 11:29:50 am by Omniso »

Luyren

• Posts: 2731
As far as I know, the drawing functions are what's taxing your performance, not the trigonometry. You are better off using the Image blocks. Might be a bit trickier. Each beam could be its own image and instance, which you would resize and rotate, basically.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Bombini

• Posts: 1397
Hi,
yes its very likely that the drawing is causing the problem.
I did some tests a while a go with drawing and also had performance issues:

My solution was using actors (you can even exclude hit boxes to avoid performance drain).
I used a system where i had a starting, middle and end actor of the laser beam. Fading that in and out had a very nice effect (not seen in the example below). I added as much length as needed per code/blocks:

You can obviously rotate them.
Cheers!

Omniso

• Posts: 192
Thanks for sharing your solution! I thought using stencyl's drawing function would be less resource intensive than using actors and was confused, thinking it was the math instead. Turns out it was the total opposite!

JeffreyDriver

• Posts: 2262
Is the drawing lag just a HTML5 issue? Or is it across all platforms?

Omniso

• Posts: 192
Just tested this a few minutes ago. The same dummy scenario I showed on my first post had 0 lag on Flash and Windows. The significant FPS drop only occurs with HTML5.

Bombini

• Posts: 1397
Across all platforms. Big impact!
Removing drawings from Windows build made it so much faster.

« Last Edit: March 19, 2020, 12:46:38 am by Bombini »

designpeg

• Posts: 731
Drawing used to be really fast, but Openfl changed the way things were drawn to the screen, I used to use drawing in my games, but now it's pretty much unusable.