Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Laser Beam  (Read 2596 times)

0 Members and 1 Guest are viewing this topic.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Laser Beam
« on: July 31, 2014, 01:41:36 am »
What is a good way to create a laser beam. I have point A and B, which can change. I also have a desired thickness. The only way I know how to create this would be to have 4 vertices, and set their world positions, so two of them are near A, and two near B, and spread out slightly for the thickness. What are some other ways of doing this? And I already found this: http://en.sfml-dev.org/forums/index.php?topic=9438.0
I don't want to have to deal with vertices ideally. Is there some object that already does what I want? Could a sprite do this?

For example, like the blue lasers here:
« Last Edit: July 31, 2014, 01:46:56 am by Strikerklm96 »

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Laser Beam
« Reply #1 on: July 31, 2014, 01:53:38 am »
Lasers are pretty simple in design. if you take a look here: http://gradius.wikia.com/wiki/Gradius_NEO?file=Gradius_NEO_Mega_Laser.png
You can see the gradius lasers.very simple design, they have a head texture (the little curvature of the laser) then they repeat the same texture for the entire length of the pre-generated length. Now if it's a constant beam like gradius/earth defense force, then you'll need to calculate it's length every frame and adjust it's size accordingly.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Laser Beam
« Reply #2 on: August 01, 2014, 10:55:34 pm »
Thanks, but that didn't tell me whether I should be using a Sprite, 4 vertices, or something else. I already knew it would have a repeating body texture, with a different image for it's head and tail.

I want to know what SFML object(s) I should be using. Also this laser can be at different angles, not just left and right facing. I would want my laser to be able to sweep 360 degrees, which is why I asked, because the trig functions for the 4 vertices of an sf::quad would be tedious.

I thought of calculating the midpoint between point A and B, putting a sprite there, setting the y scale high, so it becomes stretched across the distance, and rotating it. Is this a good solution?
« Last Edit: August 01, 2014, 11:19:25 pm by Strikerklm96 »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Laser Beam
« Reply #3 on: August 01, 2014, 11:55:03 pm »
Your rotation solution seems fine. That way you don't need to work out all of the vertices. However, you'll need to work out the rotation to be able to pass through the two points. It's simple but it's trigonometry still  ;D

You may want to take a look at Thor. It does a lot of this sort of stuff. I'm not sure if it does exactly what you want (a textures line with thickness that passes through two specified points) but if not, it'll provide all the trigonometry functions for you.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
AW: Laser Beam
« Reply #4 on: August 05, 2014, 07:10:23 pm »
Working with a sprite is essentialy just a mild hack and will probably make things more complicated in the long run.
A sprite is nothing else than a quad with a higher level API.
My suggestion would be to write your own class, specifically written for your beam. You can derive it from sf::Drawable and sf::Transformable just as the sprite, but make sure the rotation, etc. gets applied the way you imagine it. You then can also add your custom functions.

What should you use? Your own class built around vertices.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything