#Find all nodes in a line

21 messages · Page 1 of 1 (latest)

silent cedar
#

I'm working on power ups for my brick breaking game, and I'm trying to figure out a way to shoot out a line in a given direction and find all the nodes that intersect with that line so I can lower their health. What is the best way to do this?

The powerup is just an area2d with a sprite, and the bricks are staticbody2ds.

hearty sleet
silent cedar
#

Is there a way for it to keep going? From what I've read it stops at the first body

reef sable
#

If you don't care about the order of the objects ( which gets hit first) you can use an area2d to detect all objects

#

With a box collision shape with long width and small height it's similar to a line

#

Otherwise you would have to shoot multiple raycasts or shapecasts, adding the previously hit object to a growing exception list

silent cedar
#

I hadn't even thought of using a thin area2d 🤦‍♂️ but you do mention a good point about exception lists. Since I only want the line to affect each brick once, is there an easy way to keep track of this for an area2d?

reef sable
#

It's still possible to do the detection for only one frame so each brick is only affected once with an area the only thing really is that you can't determine the order of the bricks in the line that way

#

At least not cleanly or precisely

silent cedar
#

The order won't matter which is fine

#

And yeah the lines will be short lived since each ball can hit the power up, 1 frame might be too short but it's a starting point

#

I'll give it a try with both shapecast2d and area2d thank you for the suggestion

reef sable
#

a few things to keep in mind:

#

areas (and ray/shapecast) nodes need 1 physics frame to update if you move/rotate them for the new detections to be updated

#

if you cant wait one frame and want to use the area approach, or use a ray/shapecast but without nodes you can do a "theoretical" check against the current space using intersect_shape() / intersect_ray()
https://docs.godotengine.org/en/stable/classes/class_physicsdirectspacestate2d.html#class-physicsdirectspacestate2d

silent cedar
#

I like the look of that example. With that I could get the full list of nodes, update their hp, and do an animation very cleanly I think

#

And the animation would be decoupled from the actual hp logic which is nice

silent cedar
#

@reef sable Quick question regarding ray casting. It seems like even though I have a position and target position set, the angle of the ray is off (I have visible collision shapes turned on to see this). Do I need to set a rotation even if I'm telling it where to go?

#

I would expect that since the y positions of the position and target position are the same it should just be horizontal