2D Toolkit Forum

2D Toolkit => Support => Topic started by: drkucho on March 07, 2015, 08:24:41 pm

Title: updating coliders , performance
Post by: drkucho on March 07, 2015, 08:24:41 pm
i have read somewhere that if you move a collider with no rigidbody or if you move a collider with rigid body but you move it by changing its transform not by rigidbody functions you are not doing it right cause the physics engine has to recalculate the position of all colliders

if this is true, i guess that to use tk2d frame colliders is not very efficient cause as the collider has to be regenerated each frame it will cause the physics engine to recalculate everything, right?

don't get me wrong, the frame colliders is an awesome thing, but i want to know how inefficient is it for the psx engine so i can think an alternative method... or not
Title: Re: updating coliders , performance
Post by: unikronsoftware on March 09, 2015, 11:55:45 pm
Quote
if this is true, i guess that to use tk2d frame colliders is not very efficient cause as the collider has to be regenerated each frame it will cause the physics engine to recalculate everything, right?
Yes, so you should have a kinematic rigidbody on any colliders that move in any way. But you'd have that anyway if you were moving it, right?

Quote
i have read somewhere that if you move a collider with no rigidbody or if you move a collider with rigid body but you move it by changing its transform not by rigidbody functions you are not doing it right cause the physics engine has to recalculate the position of all colliders
No that isn't correct.

Title: Re: updating coliders , performance
Post by: drkucho on March 10, 2015, 01:02:07 am
but i wonder, is it correct to mod/recreate the collider of a rigidbody?
Title: Re: updating coliders , performance
Post by: unikronsoftware on March 10, 2015, 10:42:33 am
Yes its fine. As with most things its depends on what you're using it for and where. If you're doing this on all objects in the scene thats bad, but if its just for one object that should be fine.
Title: Re: updating coliders , performance
Post by: fsadeq on March 10, 2015, 01:29:31 pm
Actually, you do indeed get some spikes when moving colliders via transform instead of rigidbody.MovePosition(). At least from my profile tests. There's a physics "UpdateTransform" (or something like that, I don't recall exactly) call that is expensive (relatively speaking) that doesn't happen if you move via rigidbody. This is regardless if the collider is a trigger or not.

That said, you probably wouldn't notice anything unless you had a ton at once.
Title: Re: updating coliders , performance
Post by: drkucho on March 11, 2015, 09:23:30 pm
Actually, you do indeed get some spikes when moving colliders via transform instead of rigidbody.MovePosition(). At least from my profile tests. There's a physics "UpdateTransform" (or something like that, I don't recall exactly) call that is expensive (relatively speaking) that doesn't happen if you move via rigidbody. This is regardless if the collider is a trigger or not.

That said, you probably wouldn't notice anything unless you had a ton at once.

thanks, i have read a couple of forums and articles about this, since then i changed my code and now uses rigidbody2D.MovePosition i even made my tests with a loop of 30.000 iterations and saw the differences , thats all ok for the rigid body in the main GO, but in my hero i have different colliders as children and depending he is  walking or crouch or he is KO i need to change shape or at least move the children colliders

even if it is not noticeable cause very few colliders to be altered, i am worried about the garbage collection , i am rewriting my code to avoid the GC collect call that happens sometimes and in my game, for unknown reason is making a huge spike.(http://i59.tinypic.com/o552io.png)

im not expert so i don't know how to read the profiler information exactly but seems that the garbage is generated cause 59 calls that change collider transform in my player script ...? is that correct?

i need to clean this as i plan to use the same script for other 20 or 30 characters in the game all running at once...
Title: Re: updating coliders , performance
Post by: unikronsoftware on March 12, 2015, 11:12:14 am
There is no garbage generated by that, GC Alloc = 0.
You should deep profile to get more info.

Your game.update script is generating a fair bit of garbage though.