+6 votes

I have a sprite on a sheet that has different size depending on the frame. Is there a way to have collision shape change depending on what frame it is on?

Some game engines allow a black and white image to be added to a sprite sheet as a collision mask. Is there anything like this in Godot?

in Engine by (356 points)

I don't know if there is an easy way to do it, but if you manage to do it, maybe it will be fine for static objects... but issues could happen on kinematic or rigidbodies. Physics engines rarely like their shapes to change or teleport into something else every frame.

Darn. How do you handle a sprite collision when the character ducks and the collision shape needs to be smaller?

I didn't say it doesn't work, just that there might be issues.
For kinematic chars you could try, but probably will be more efficient to set a collision shape per state, and not per frame (because you could have many frames for the animation)

So how would you do that? Do you create multiple shapes and programmatically disable them as needed?

Yes. To do what you asked, you would need to do that anyways first before being able to do that per frame (which is basically what it would do)

I have exactly the same problem: KinematicBody2D player needs a different collision rectange when it walks VS when it ducks VS when it swims. I have a separate CollisionShape2D for each state, and I use CollisionShape2D.disable so that only one of them is active at a time. It works, except, because the effective collision shape changes suddenly, it might will overlap with another collision shaple (like a wall tile or enemy), and the physics engine doesn't allow that. So usually it automatically teleports the character on some direction to get rid of the overlap. This teleportation sometimes leads to comical glitches, since the engine can't know what makes sense gameplay wise. Worse, sometimes it lefts the player stuck inside the wall... There must be a common way of handling this... So I also wonder what's the best practice.

Since then I have realized that this kind of problem pops up in an even more basic case: if you flip a KinematicBody2D (like as the actor moves left or right), its CollisionShape2D isn't flipped by Godot. (So it's better be symmetrical...) If it would be flipped, they run into the same issue (changing collision shape, might overlaps with another suddenly). Now it starts to be absurd... I must be blind or something.

3 Answers

+1 vote
Best answer

Here is an answer to my own question. I hope this helps those trying to change the shape on the fly. If your shape is called "shape" you use:

$shape.get_shape().set_extents(Vector2(59, 59))
by (356 points)

Hello,
I am currently having the same problem but I don't understand how your example solved it exactly. What is the vector being used for? Also how/where would you incorporate this line of code into your program? I have four different hit-boxes (made with collisionpolygon2D) but I dont know how to attach them to the different animations. Any help or insight would be really appreciated.

+8 votes

If you are using an AnimationPlayer node you can easily set any parameter of your CollisionShape

by (203 points)

We should mark this one as the answer to the question

We should mark this one as the answer to the question [2]

0 votes

If you're still having trouble (I know I'm like a year late in 2019) then try using a raycast2D pointing above the character in that goes a little above it's maximum height. Then use something like : if $RayCast2D.iscolliding() == false:
$CollisionShape2D.disabled = false
$CollisionShape2D2.disabled = true
in the code. The collision2D2 is the smaller one and the other is of course larger. Also the raycast will know when the ceiling is hit. I also used:
if Input.is
actionpressed("uiup"):
if isattacking == false:
if on
ground == true:
velocity.y = JUMPPOWER
on
ground = false
$CollisionShape2D.disabled = true
$CollisionShape2D2.disabled = false
elif is_falling == true:
$CollisionShape2D.disabled = false
$CollisionShape2D2.disabled = true
I've tried it so far at it always seems to work. My character wasn't getting stuck in the ceiling.

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.