Game Development Community

Laser Ray

by yurembo · in Torque 2D Beginner · 03/22/2014 (11:28 am) · 6 replies

Hello everyone!
How to make a laser ray from a defined point to the bounds of the screen with a correct angle?

#1
03/23/2014 (12:12 am)
This one's tough since there aren't pixel/line routines exposed to TS. What we do have is sprites and vectorshapes. You can either create a sprite and scale/rotate/translate it or create several to form a line. Example: https://github.com/practicing01/Dots_and_Crits/blob/master/modules/Cards/Laser/assets/scripts/Update_Laser_Beam/Update_Laser_Beam.cs

In a pseudo-nutshell:

1) Sprite_Laser.setSize( Vector2Distance( Vec2D_Laser_Origin, Vec2D_Laser_Target ) )
2) Float_Angle= Vector2AngleToPoint( Vec2D_Laser_Origin, Vec2D_Laser_Target )
3) Sprite_Laser.Angle= Float_Angle
//The following is to offset because sprite origins are at the center, not top-left
4) Vec2D_Projected_Offset= Vector2Direction( Float_Angle, Laser_Size / 2 )
5) Sprite_Laser.Position= Vector2Add( Vec2D_Laser_Origin, Vec2D_Projected_Offset )
#2
03/23/2014 (12:29 am)
The AngleToy in the development branch also shows off some methods to draw lines depending on the cursor location.

github.com/GarageGames/Torque2D/blob/development/modules/AngleToy/1/main.cs
#3
03/23/2014 (1:00 am)
Thank you, guys, so much!
One more question:
How to find coordinates of a destination point in some distanse of a source point in which direction this looks up?
#4
03/23/2014 (1:33 am)
Can particles collide with other objects?
#5
03/23/2014 (3:05 am)
a) trignonometry is your friend and it's way too early in the day for me to answer that coherently.

b) Particles cannot collide with other objects. I suggest that you check out Liquidfun, which adds Box2D particle collisions and other neat stuff, such as fluid simulation.

Bear in mind though that even though this system adds collidable particles, it does not tie in automatically to the existing T2D particle effects system.

#6
03/23/2014 (9:48 am)
Thank you @Simon!
a) I forgot about a power of Pythagorean theorem )