Question about casting rays (bump.lua)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Question about casting rays (bump.lua)

Post by MaxGamz »

I’m trying to make a raycastor. I understand the concepts but I want to try to make mines a different way. For one, I’d like my rays to use collision, so I’m using bump.lua for my project. However, I’m having trouble drawing or creating rays long enough to hit the collidable walls. I tried searching for ways to query the world but it seem like the best way to do it is with bullets, however I want a constant laser beam, not bullets, to achieve my effect. Is there any advice you guys have?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Question about casting rays (bump.lua)

Post by MrFariator »

Depends on how you want your laser to behave. If you want your laser to move, and it will be an axis-aligned bounding box, then you can just create a thin rectangle that either moves on the x or y axis. The only problem may be handling how to handle collisions, or the laser "shrinking" after striking a surface, assuming the laser moves like a projectile.

If you want to handle a laser that may have a fixed starting point, but keeps going until it hits a wall or similar, then you can use the world:querySegment function:

Code: Select all

local items, len = world:querySegment ( laserStartX, laserStartY, laserEndX, laserEndY, yourFilter )
-- items table will contain items sorted by distance from laserStartX and laserStartY, if any hits were detected
You can also approximate querySegment by giving the laser a small rectangle collider (like maybe 1x1, 2x2, 3x3 or similar). Whenever you are checking the laser, you keep the laser in a fixed location, and then use the world:check function. This is because world:check will return you collisions, but it will not update the collider in the world like world:move would (you'd need to call world:update separately, which you may not necessarily need for a laser with a fixed starting position). Because world:check returns the coordinates where the laser would have ended up, you can use that to influence your rendering. Something like:

Code: Select all

local actualX, actualY, cols, len = world:check ( laserObj, laserGoalX, laserGoalY, yourFilter )
laserEndX, laserEndY = actualX, actualY -- store the coordinates in some manner, make sure not to call world:update with the object unless you want to change its starting location

-- when rendering the laser
love.graphics.line ( laserStartX, laserStartY, laserEndX, laserEndY )
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: Question about casting rays (bump.lua)

Post by MaxGamz »

MrFariator wrote: Mon Feb 12, 2024 1:39 pm Depends on how you want your laser to behave. If you want your laser to move, and it will be an axis-aligned bounding box, then you can just create a thin rectangle that either moves on the x or y axis. The only problem may be handling how to handle collisions, or the laser "shrinking" after striking a surface, assuming the laser moves like a projectile.

If you want to handle a laser that may have a fixed starting point, but keeps going until it hits a wall or similar, then you can use the world:querySegment function:

Code: Select all

local items, len = world:querySegment ( laserStartX, laserStartY, laserEndX, laserEndY, yourFilter )
-- items table will contain items sorted by distance from laserStartX and laserStartY, if any hits were detected
You can also approximate querySegment by giving the laser a small rectangle collider (like maybe 1x1, 2x2, 3x3 or similar). Whenever you are checking the laser, you keep the laser in a fixed location, and then use the world:check function. This is because world:check will return you collisions, but it will not update the collider in the world like world:move would (you'd need to call world:update separately, which you may not necessarily need for a laser with a fixed starting position). Because world:check returns the coordinates where the laser would have ended up, you can use that to influence your rendering. Something like:

Code: Select all

local actualX, actualY, cols, len = world:check ( laserObj, laserGoalX, laserGoalY, yourFilter )
laserEndX, laserEndY = actualX, actualY -- store the coordinates in some manner, make sure not to call world:update with the object unless you want to change its starting location

-- when rendering the laser
love.graphics.line ( laserStartX, laserStartY, laserEndX, laserEndY )

I have a question, for raycasting to work, do I need the rays to keep goind at a distance until they hit an object (so theoretically an infinite ray of there are no objects around) or is using a fixed length ray enough?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Question about casting rays (bump.lua)

Post by MrFariator »

Again, depends on how you want the lasers to work. As in, are they fixed lasers that are always enabled (like a level hazard with fixed positioning), or are they a type where they become longer over time, until they hit a wall or an object (eg. a laser turret that goes on and off on a timer or a button press)? If they do become longer over time, do they ever dissipate, effectively becoming 0 length until they are re-enabled? What objects do the lasers need to interact with (player, enemies, walls)? Are there any mirrors that might redirect the lasers' paths (like Portal)? How are you planning on dealing with any collisions that happen off-screen? These are all questions that might influence how you want to go about things.

If you don't know ahead of time how long a laser might wind up becoming, I recommend just picking some specific length that covers a screen or two. Like if the width of your game's screen is 400, maybe pick 800, or 1200. If they are fixed, unmoving lasers, you might even be able to precompute the length. Just going out to infinity isn't exactly feasible in bump.lua in my opinion, because you might start hitting some performance (or even game design) concerns if you scale things up enough. Determine how you want the lasers to (reasonably) work first, before concerning yourself with infinities.

If you actually need truly infinite raycasts, then bump.lua may not be the best tool for the job.
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: Question about casting rays (bump.lua)

Post by MaxGamz »

MrFariator wrote: Wed Feb 14, 2024 4:52 am Again, depends on how you want the lasers to work. As in, are they fixed lasers that are always enabled (like a level hazard with fixed positioning), or are they a type where they become longer over time, until they hit a wall or an object (eg. a laser turret that goes on and off on a timer or a button press)? If they do become longer over time, do they ever dissipate, effectively becoming 0 length until they are re-enabled? What objects do the lasers need to interact with (player, enemies, walls)? Are there any mirrors that might redirect the lasers' paths (like Portal)? How are you planning on dealing with any collisions that happen off-screen? These are all questions that might influence how you want to go about things.

If you don't know ahead of time how long a laser might wind up becoming, I recommend just picking some specific length that covers a screen or two. Like if the width of your game's screen is 400, maybe pick 800, or 1200. If they are fixed, unmoving lasers, you might even be able to precompute the length. Just going out to infinity isn't exactly feasible in bump.lua in my opinion, because you might start hitting some performance (or even game design) concerns if you scale things up enough. Determine how you want the lasers to (reasonably) work first, before concerning yourself with infinities.

If you actually need truly infinite raycasts, then bump.lua may not be the best tool for the job.
Thanks for the advice! I'll test around and see what I'm most comfortable with
Post Reply

Who is online

Users browsing this forum: No registered users and 52 guests