# Laser

0 favourites
• 19 posts
From the Asset Store
A sound pack containing 132 laser gun sound effects, including mono and stereo versions of audio files.
• I am sure this will be helpful for many.

I want to make a laser, almost exactly like the one in the 'laser' version of Ghost Shooter. However the fundamental difference is that I want the laser to keep going until it hits the nearest object, not the mouse. There are currently 2 specific objects, might be more.

Any help? also of note one of the objects exists all over the map in different sizes.

Basically I need to find the distance in a straight line from one object to another at a specific angle.

• you could try the event: "Is overlapping another object".

the laser would go from your player to infinity, but it will recognise if something is in the way. if i understand your problem correctly this is allready the solution to your problem. you don't need to calculate the distance.

if you need the distance, you need the x and y coordinates of the object your laser is crossing and calculate the difference to the player.

!! i hope that i don't write nonsense

let's say the player is at:

x = 100

y = 100

and the enemy or obstacle ist at:

x = 215

y = 312

now you have to do some math:

at first you calculate the difference between the x and y coordinates

215 - 100 = 115

312 - 110 = 212

now you need our good friend pythagoras

calculate the square root of (115^2 + 212^2)

the solution to this is 241,18

_______________

\|(115^2 + 212^2)

edit:

so much for my ascii art <img src="smileys/smiley1.gif" border="0" align="middle" />

• I think the problem lies in the logic. The laser chooses its length by stretching, however it do not think it is possible to find this X and Y value of the collision.

You say you need 'the X and Y values of the object you are crossing' however if this object is stretched out significantly and the laser want to hit the edge, you will have a problem.

• There's a distance expression in C2 that automatically clculates well distance from X1,Y1 to X2,Y2 :)

And it should work fine but that's only half way, after "distance" you will have laser end in the middle of overlapped object.

Can be problematic if you use "on overlaping" cause there's only simple bounding box collisons for now.

There is somewhere on the forum cap file for CC with example of instant laser, but as i can remember it usses bullet behavior with "On step" condition - whitch is not present in current C2 bullet behavior.

• No need to use distance, just place a spite where you want to shoot from and set it's angle at what you want to shoot at. Then increase the width until it collides with a wall.

http://dl.dropbox.com/u/5426011/c2/laser.capx

• R0J0hound... That is a great solution! after seeing your solution, however, I think I have found a better way to do it that involves less performance drop and more accuracy (it does not at all stick into the walls.) I think all this is is a minor improvement on your way of solving it.

OK, so we have an instance variable for the laser called increment. It starts at 1000 or any value that you want to be the max range.

We start our loop.

If the laser does not collide with any walls, set the width to width + increment.

If the laser does collide with a wall, set the width to width -     increment.

divide increment by 2.

Loop breaks when increment is less than or equal to one.

That is only 11 calculations per step (or 12 if you want a longer range of, say, 2000).

I think this may actually be the best solution to the problem. I hope this helps people with their laser issues!

Here is the improved example: dl.dropbox.com/u/41931267/Improved%20example.capx

• I saw no performance drop in rojo's capx.

As a matter of fact, your capx requires more calculation than his.

He tests for 1 collision of the laser object 30 times in a tick.

You do look 11 times a tick as long as increment is superior than 1 for 4 collisions of the laser object with 2 different object types.

Rojo do it in 2 events, you did it in 7.

So for accuracy, indeed your laser has the right length, but performance-wise, rojo's capx consume less ressources.

• I also saw no performance drop but in a big game every bit counts, especially if you want pixel-perfect accuracy (as in reaches all the way to the wall but does not go in it). In Rojo's example if you want pixel-perfect accuracy you are performing a collision check for every pixel of the distance until the laser reaches a target.

Well the two different object types were for my own purposes, if this was in Rojo's example it would also consume more events.

I understand what you are saying and I do not want to turn this into a contest, but by my maths if the object is more than 220 pixels away the first example is performing as many calculations as mine but still displaying a 20 pixel inaccuracy...

The first method is certainly simpler and good if you do not need accuracy however I believe (I may be wrong) that a couple more events is less consuming than hundreds of collision checks per tick for greater accuracy.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Gee, my last post seems rather critical. Just to be clear, Rojo's method was the main base for my small improvement and I could never have done it without his excellent example. Thanks man!

• I'm trying to follow along with this, while I can re-create it, my intention in the game is to have an enemy which spins the laser around itself. How would I do this with multiple enemies, 'for each' doesn't seem to do it right.

• CrudeMik Did you try putting the laser in a container with the enemy?

• ooh, I have not. I keep forgetting to learn about containers! Thanks

• sqiddster I don't suppose you still have this do you? The dropbox link is broken. I have the version R0J0hound created set up, but notice that it's doing around 400 collision checks a tick.

I plan on having 4 characters running around with these lasers, so I'm interested in checking out the other method to see if it produces less checks.

I tried recreating it using what you've described in your post, but don't see where the increment variable is reset. As far as I can tell, it just reduces down to 1 and then stops.

I know we're now 3-4 years down the line...

• Maybe this one can help?

• AnD4D Hey, yeah, I just cleared out my Dropbox recently. Had to happen eventually, I'm afraid.

Anyway, I actually have a much improved version! Lightning-fast raycasting

• 19 posts