# Laser

Get help using Construct 2

### » Sat Sep 17, 2011 5:01 am

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.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Sat Sep 17, 2011 7:11 am

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
http://en.wikipedia.org/wiki/Pythagoras#Pythagorean_theorem

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 Pete2011-09-17 07:14:33
B
11
S
3
G
2
Posts: 15
Reputation: 1,569

### » Sat Sep 17, 2011 8:11 am

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.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Sat Sep 17, 2011 1:32 pm

@Pete
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.

@sqiddster
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.
B
160
S
69
G
44
Posts: 2,611
Reputation: 36,844

### » Sat Sep 17, 2011 7:34 pm

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
B
100
S
38
G
134
Posts: 5,556
Reputation: 85,325

### » Sat Sep 17, 2011 10:52 pm

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: http://dl.dropbox.com/u/41931267/Improved%20example.capx
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Sat Sep 17, 2011 11:27 pm

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.
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
311
S
128
G
101
Posts: 7,414
Reputation: 75,224

### » Sun Sep 18, 2011 4:13 am

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.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Sun Sep 18, 2011 4:36 am

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!
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Mon Feb 04, 2013 10:23 pm

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.
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

Next