|
|
|
|
Reply From: |
Zylann |
In Godot 2, shapes are not nodes when you run the game. CollisionShape
nodes are editor-only helpers, even if they are present when you debug it, they are stripped out when exported so you should not rely on them from script.
Instead, the actual shapes are accessed using an API on CollisionObject2D
: CollisionObject2D — Godot Engine (2.1) documentation in English
Note that Area2D
, RigidBody2D
or StaticBody2D
inherit CollisionObject2D
, so they have these functions too.
As you can see, there are functions to query shapes from their index, which is what you get in your function.
I never used this, but I guess you would have to do do something like:
if body extends Area2D:
var shape = body.get_shape(area_shape)
Thanks, that was what I was looking for (or I thought so).
Even though I have used node.get_shape().get_extents()
, I had no idea get_shape()
could accept a shape index as a parameter.
It looked promising!
Unfortunatly it does not to solve my problem. I would need to detect which shapes are overlapping based on the names or something like that - and shape .get_name() is not returning anything (cause I guess like you said the actual shapes are not the editor representation).
So in the end what I need is not the shape itself, but the node “editor-only helper” of it.
cardoso | 2017-05-08 20:47
If you need to identify them, you can probably do it by their index. If the shape is child number X, its index might be equal (“might” because I never tested it).
Another way is to use layers to separate collision detections (but you would have to change your code a bit).
Or, if you really want names, make two Area2D.
Zylann | 2017-05-08 22:26
Yeah, I ll go with two Area2D.
I am currently using the index to identify them. But seems very easy to “break”, if new shapes are created/removed.
Thanks
cardoso | 2017-05-08 23:47
I noticed that if you get the shape, you also have to account for the parent body’s scale. So it is not enough to get the shape, you might have to multiply something like its radius (for a circle) by the parent’s scale.
Judd Gledhill | 2018-08-24 01:15
Note: this answer was for Godot 2, I edited the post now. In Godot 3 you can access the shape nodes directly.
Zylann | 2018-08-24 18:13