Raycast with more information

Copied from discord post.

How do I check what is on top of a the block

Hello.

I want to know without a grid system what blocks are stacked on each other.

I have tried working with a point system. But that not working because the blocks can rotate. And it has influence on the sprite measurements.

So I tried raycasting.
But If I want to use the easy group method for checking it’s stuck on itself.

And if I raycast above it’s own. Then it only gives back the first hit. Or the last hit.

But with the last hit it also takes the first block if there are not other blocks on top. So that gives false positives.

What I would like is to have an raycast. Of a specific length. Then it returns the name of anything it hits in those rayscast.
The raycast now only gives the x and y back and then its also need extra a actions to know what it hits.

Other thing I was thinking.
If I can pick a object that is between the x of the block.
And then with the y of the stack order.

So then for every block it picks the 3 objectives above it.
But also with the pick nearest. You need to tell it can only pick between the lines of the x. And the y stack order. If there is nothing there it should not pick anything.

From the box perspective I want to know what other boxes are on top of it.

Its a free build stacking game where the blocks have interaction with each other.

This is the gamejam version what I made in defold.

Trying to remake it here.

(upload://1gyqRdscA6RMWZ3KCmQ4LwzA0Ee.jpeg)

The Image didn’t upload.

If I understood correctly, you are trying to detect the blocks on top of a specific block.

Assuming your game is 2d,
I am thinking of using a sprite for the sole purpose of detecting blocks.
Create this detector sprite in the center of the block and increase by the height of block each iteration.
Every time you move it up, check if it collides with a block if it does, add one to a counter, if it doesn’t stop iterating.

This is what I mean:
Showcase

Note: This won’t work if you want to detect all blocks above a block if there were gaps between them!

Note: This assumes all blocks have the same dimensions(size).

Your image isn’t showing. More context would help. Are the boxes physics objects? Do you need a list of the objects or a collision check between the objects. Like between boxes 1, 2 then 2,3 and so on.

What happens when they’re stacked.

Thanks

Hopefully the img work this time.
I am not aloud to post the its io link i have of a working build from defold.
You can find it on discord or pm me.

But to be more clear.

Its indeed 2D sprites with collisions.
Atm there the same size in the futures different shapes will be added.

For example
For the wood bloks
If there are 2 stone blocks on a wood block the wood block will crack.

If there are 3 stone blocks on a wood block its breaks and destroyed.

For the glass blocks
When it has a stone directly on top it breakes

When it had a wood and then 1 stone it cracks
When it has a wood and 2 stone it breaks.

1 Like

Offcourse more combination can be createrd later but for everything i need to know how the blogs are stacked.

On defold i do this with a raycast of the length of 3 blocks. Then i get back in a table the ID of everything the raycast hits.

Is there’s a way for me to check how the current raycast works and see if i can play with that a bid more.

You can look at the Javascript functions.
I think point is inside might be easier. You can use the the X, Y minus the object height times how many blocks away.

Another option would be a hitbox object used for testing.

IDK if it would be easier to test things as boxes drop and collide or scan the stack from the bottom up each time.

It might even be useful to use object variables for things like weight, maxWeight or maxWeight per block type. IDK just brainstorming.

This is the raycast

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/functions/gdjs.evtTools.object.raycastObject.html

It’s defined here
https://github.com/4ian/GDevelop/blob/49749fbd88b63db1f9f342a01097022015a26f69/GDJS/Runtime/events-tools/objecttools.ts#L440

Thanks for sharing.

I think there is a way to create a new function that gives a list of objectives back it hit. because i see it natively is creating a list of objects hit.

So i need to dive into it because this is new for me.

Or if someone with more experience see this and thing. That would be workable for me would be great

I have worked with the point system first that gave best results only the block then cant rotate or els the points won’t work.

Atm also am trying the other idea of creating a object on set location and identifying a hit.

Just to update i am going to implement the point systeem or the spawn something to check update for this.

However also interested in checking if i could make this multi hit raycast sometime

One issue with raycast is that it could miss a block that is hanging more than halfway off the stack, to avoid this you would need at least two rays (sufficient if the blocks are always wide enough).

Personally, I would go for the invisible hitbox solution anyway, which seems easier in terms of object-picking. There’s many ways to go about it, here’s one that just popped in my head:

  • Link hitbox to block that is being checked
  • As the hitbox moves up and collides with blocks, add their weight to some variable like totalWeight.
  • When the hitbox is done colliding, compare the totalWeight to the linked block’s strength

You might want to use the object picking tools extension, which provides an action to un-pick objects (I feel like this should be integrated into GD, has solved many problems for me…)

Thanks all for all the repiels

I am going for the spwn checker and doing this per block type via external events makes it better workable :smiley:

This is the wood that check its stone stacks

happy to share i got it working :smiley:

https://cdn.discordapp.com/attachments/1178704775118864405/1351634191120601168/it_works.mp4?ex=67db16ef&is=67d9c56f&hm=460c3762f25fb27f2e290cdf87dafff73fd5070bef890cc08c6ff40b7b76cd0e&

1 Like