I find weird that Shape2d is a resource ...

DredokDredok Posts: 11Member
edited August 2016 in 2D
Hi,<br /><br />Today I tried to change the extents of a RectangleShape2D in runtime, and I noticed that every other instance of the scene which contained the shape changed the extents too. Then I saw that Shape2D is a resource, so everyone is pointing to the same shape. Understood.<br /><br />That is a bit awkward-at least to me-.<br /><br />This problem arises from my use case:<br /><br />In my game, there are some items with two states:<br />first state: just waiting to be touched<br />second state:  activated <br /><br />When these items are in first state, I want to have the collision shape larger than when they are in second state. That's because I want to make easier to touch them. <br />How would you solve this?  I think having two bodies would be ugly ...<br /><br /><br /><br />

Tags :


  • MegalomaniakMegalomaniak Posts: 3,003Admin
    So use/write a state machine to handle it(say set scale of the collider)?
  • DredokDredok Posts: 11Member
    i'm fine handling the states, the issue is to change the physics in a proper way.<br /><br />You suggest to change the scale of the collider but the documentation states that is should not be done:<br /><br />http://docs.godotengine.org/en/latest/tutorials/2d/kinematic_character_2d.html<br />
    <br />Note: As mentioned before in the physics tutorial, the physics engine can’t handle scale on most types of shapes (only collision polygons, planes and segments work), so always change the parameters (such as radius) of the shape instead of scaling it. The same is also true for the kinematic/rigid/static bodies themselves, as their scale affect the shape scale.<br />
    <br /><br />But as I said, If I change the extents of my shape, as it is a resource, it will propagate to all instances of this shape.<br /><br />I would like to be sure that the proper way is to scalate the collider (kinematicbody2d) as you suggest, because the documentation seems to say otherwise
  • RossRoss Posts: 199Member
    Changing the extents of the shape has worked for me, you just need to instance it in the ready function so they don't all change together. Something like this: <br /><br />
    func _ready():<br />	if is_inside_tree():<br />		clear_shapes() # I found this necessary, for some reason there were 3 shapes already there for no reason. <br />		add_shape(RectangleShape2D.new())<br />		var extents = Vector2(15, 25) # or whatever<br />		get_shape(0).set_extents(extents)
  • DredokDredok Posts: 11Member
    Thanks for your replies guys. <br /><br />I found another piece of documentation which states that scaling a curved physics shape non-uniformly is not advised but is ok to scale non-curved shapes. So to fix my issue I will follow the Megalomaniak approach to just scale the collider (kinematic2dbody).<br /><br />Just for reference, I link the other piece of documentation<br />http://docs.godotengine.org/en/latest/tutorials/2d/physics_introduction.html#when-problems-begin<br /><br />
    Transforming shapes<br />As seen before in the collide functions, 2D shapes in godot can be transformed by using a regular Matrix32 transform, meaning the can check collision while scaled, moved and rotated. The only limitation to this is that shapes with curved sections (such as circle and capsule) can only be scaled uniformly. This means that circle or capsule shapes scaled in the form of an ellipse will not work properly. This is a limitation on the collision algorithm used (SAT), so make sure that your circle and capsule shapes are always scaled uniformly!
    <br /><br /><br />I think the documentation I linked in the first post should be reworded because it can be misleading.
  • MegalomaniakMegalomaniak Posts: 3,003Admin
    To be fair I used(meant to anyways) the word 'scale' rather generically, so if scaling the node size doesn't work but scaling the physics radius does then far as I'm concerned that is a perfectly viable method of scaling too. ;)<br /><br />Most important is for you to to be ready to experiment and find the solution that works for you after all.
  • DredokDredok Posts: 11Member
    If you read carefully my posts, I said that I can not change the extents because shapes are resources and as such it would change the extents of all other instances. Then you suggested to scale it, I quoted a missleading part of the documentation where clearly states to not scale it. After a while I spoted another place of the documentation -also quoted in this thread- which says that scaling shapes is fine if they are not curved (or curved if the scale factor is uniform).<br /><br />So scaling the body is fine, because the body is a node. Changing the extents / radius IS NOT AN OPTION because they are resources and will affect the rest of the references.<br /><br />Currently there are only two possibilities:<br />- instanciate the shapes at runtime as Ross suggested <br />- or scale the body (and be careful the shapes are not curved if the scale is not uniform).<br /><br />BTW. I like experimenting, I don't like confusing documentation, so I try to reflect that there is some ambiguity here. I have experience with other engines and the physics parts can become tricky quickly, so knowing the best practices is better than trying to hack things until they somewhat work, I personally find this a recipe for disaster in physics part of game development.<br /><br />anyways thanks for the help to both of you :)<br />

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file