Change collision object/shape values from script

Hi,

I wonder if it is possible to access and modify values like dimension and position for shape objects through code. I need to set those dynamically from script because dimensions and position depend on player’s input. Only other solution I can think of is making a factory and prototype object for all possibilities and spawn one that corresponds to player input. But there are too many of them and it would affect performance dramatically.

Also, I have one more question that may be related to this. Is it possible to add component to game object from script? For example, I want to add that collision object when player does some action, instead of adding it manually.

Thanks in advance
Cheers
Danilo

1 Like

You cannot set the shapes or dimensions of the collision components at runtime.

And, currently, you cannot change the scale of the physics object by scaling the game object either. We have an issue for that: https://forum.defold.com/t/physic-body-scale-issue-def-521

There is a workaround, and that is that you can set the scale when creating the game object via a game object factory.

ANd, no yo cannot add components at runtime. You can enable/disable components though: https://www.defold.com/ref/go/#disable

4 Likes

Thanks very much for reply. I will try to make it work with that workaround.

Is there any chance this could become a feature? I’m currently working on a game where it would be a huge benefit to be able to support this. Otherwise, I’m forced to create multiple triggers w/ all the range values I want to support and dynamically disable/enable them… Otherwise, perhaps if the Box2D tree could be exposed to scripts so that we could efficiently locate nearby objects somehow without writing our own data-structure in Lua (which would likely perform badly in comparison.)

Yes, maybe. One of the problems we have is how to a reference something inside a component. In this case you want to reference the individual shapes of a collision component, in other cases it may an individual emitter of a particle effects. We have discussed ways to extend URLs to also include things inside a component:

world:/some/game/object#component:shape_id

The above is only part of the problem we need to solve. And it could also be something like:

physics.set_box_size(" world:/some/game/object#component", "shape_id", vmath.vector3(w, h, d))

Once we have agreed on the above we must consider the implications of changing shape dimensions at run-time. Does Box2D and Bullet support this? Or do we have to delete the shape and recreate it?

I don’t think we have this as a feature request on GitHub. Please go ahead and create it!

3 Likes

Thanks for the response, I’ve created a feature request: https://github.com/defold/defold/issues/5239 :heart:

Box2D doesn’t support it, so the shape/fixture would have to be deleted and recreated. This works well with static or kinematic bodies, but dynamic bodies become more complicated because simulated properties like friction, inertia etc would need to be transferred to the new body.

3 Likes

Would triggers be doable? I’m wondering how the enter/exit messages would be affected in particular in that case. If the range expanded, it likely shouldn’t trigger an enter on objects that were in the previous range so that makes it complicated.

This issue (setting properties of collision objects) was closed soon after it was opened, but I don’t see how setting properties is achieved through the API. Is it in fact only ‘implied’ scaling that works, i.e. the collisionobject respects scaling of its game object?

For new projects (which has the setting physics.allow_dynamic_transform set), the collision objects inherits the scale of the game object.

As for exposing “properties” in general, I think we need a proper use case.
SImply exposing all properties will be daunting, and probably unnecessary.

2 Likes