Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411560 Posts in 69384 Topics- by 58443 Members - Latest Member: junkmail

May 03, 2024, 04:09:58 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to do lasers?
Pages: [1]
Print
Author Topic: How to do lasers?  (Read 2315 times)
PureQuestion
Level 4
****



View Profile
« on: April 15, 2010, 12:49:44 PM »

Alright. Currently trying to program my boss rush game, and running into an issue. Specifically, I can't figure out how to create an effective laser that can fire in any direction. So I'm turning to you for help! yes, you! Right there!

So yeah, anyone willing to tell me how this thing is done?
Logged

Kadoba
Level 3
***



View Profile
« Reply #1 on: April 15, 2010, 01:37:53 PM »

draw_line()?


Logged
AndrewFM
Level 2
**


whoops


View Profile WWW
« Reply #2 on: April 15, 2010, 02:01:17 PM »

If the laser is a sprite:

To rotate the sprite:

Code:
image_angle = //An angle from 0-360; the sprite will rotate in this direction, assuming the default sprite's orientation is facing to the right

To move it around:

Code:
direction = //An angle from 0-360; the object will move in that direction
speed = //The speed of motion. A negative number will make the object move backwards

If the laser is a line:

Code:
/*
xx - The X position of the laser
yy - The Y position of the laser
size - The length of the laser, in pixels
dir - An angle from 0-360, corresponding to the rotation angle
*/
draw_line(xx,yy,xx+lengthdir_x(size,dir),yy+lengthdir_y(size,dir));

I think that's what you were asking... your question was a little vague.
Logged
lasttea999
Level 2
**


View Profile
« Reply #3 on: April 15, 2010, 03:40:55 PM »

You'll probably also need to calculate collisions with the line, right? There's a function for that, something like "collision_line()". Try to look it up in the GM help file.
Logged

PureQuestion
Level 4
****



View Profile
« Reply #4 on: April 15, 2010, 04:08:23 PM »

The laser is in fact a sprite. I'm starting to wonder if what I had in mind is actually possible. The idea is that it will create duplicates of itself in a straight line in any direction, instantly, until it hits a wall/the edge of the screen, forming a solid instant laser. The problem is having it create objects in a set distance in any direction.
Logged

AndrewFM
Level 2
**


whoops


View Profile WWW
« Reply #5 on: April 15, 2010, 04:16:43 PM »

When you use instance_create(), it also returns a reference to the object you created. You can store that in a variable, and use that variable to do operations on the new object.

For example:

Code:
direction = 50; //objectA's direction

newObj = instance_create(x,y,objectB); //Create an instance of objectB, and store its reference in newObj

newObj.direction = direction; //set objectB's direction to the same as objectA's direction
Logged
necromian
Level 2
**

ʘ_ʘ


View Profile WWW
« Reply #6 on: April 15, 2010, 04:23:05 PM »

It's not too complicated. I'll make an example later if I have time, but for now I'll try to quickly explain it.

I used this method to create this laser effect


Basically you make a sprite for the laser with a width of one or two pixels (the height can be whatever you want)

Make the object that shoots out the laser.

Calculate the distance in the direction it's facing to a wall using whatever method you want. Collision_line, or do/until loops are probably the best way.

Make as many laser segment objects as necessary to fill the gap between the object and wall. Place them along the intended direction using lengthdir codes. Make sure to give them an initial image_angle of the direction the laser making object is facing. You can do that with this code -
Code:
i = 0;
repeat length_until_collision_with_wall
{
laser = instance_create(x+lengthdir_x(i*laser_sprite_width,angle),y+lengthdir_y(i*laser_sprite_width,angle),obj_laser);
laser.image_angle = angle;
i += 1;
};





Logged
BlueSweatshirt
Level 10
*****

the void


View Profile WWW
« Reply #7 on: April 15, 2010, 06:45:52 PM »

There's a more efficient way.. Much more efficient.  Droop

Like above, create a sprite with one pixel width-- Height is whatever you want.

Create an object and set it's image_xscale variable to whatever you wish. If you want it to run infinitely and stop at obstacles, a check like collision_line() might be called for.
But, from GM7 onwards, image_xscale and image_yscale are taken into consideration when calculating collisions-- which means you can just check collisions regularly when using this method. It's much more efficient than creating a clusterfuck of segments.  Hand Thumbs Up Left
Logged

necromian
Level 2
**

ʘ_ʘ


View Profile WWW
« Reply #8 on: April 15, 2010, 07:33:00 PM »

There's a more efficient way.. Much more efficient.  Droop

Like above, create a sprite with one pixel width-- Height is whatever you want.

Create an object and set it's image_xscale variable to whatever you wish. If you want it to run infinitely and stop at obstacles, a check like collision_line() might be called for.
But, from GM7 onwards, image_xscale and image_yscale are taken into consideration when calculating collisions-- which means you can just check collisions regularly when using this method. It's much more efficient than creating a clusterfuck of segments.  Hand Thumbs Up Left
That works for something like a solid color laser, but if he can't use draw_line code for the beam, I assume he wants it to look more like the laser beam I posted above.

Your code stretches the image, while mine keeps the original intact, which makes the image a lot clearer if you want to put little flourishes or animations on the beam.
Logged
BlueSweatshirt
Level 10
*****

the void


View Profile WWW
« Reply #9 on: April 15, 2010, 08:02:40 PM »

Well, you could always draw a different sprite tiled, and draw that tiled rather than drawing the original sprite stretched.

Creating multitudes of objects like in your example can be very inefficient, especially in games with lots of things going on.(then again-- your image suggests that only 5 objects were made for the laser, which isn't nearly as bad.)

Just some friendly advice. Smiley
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic