Reset objects collision so OnCollisionEnter triggers again - unity3d

We have an unusual use case. Let's say you have 2 objects that is touching. This will trigger OnCollisionEnter. Now we change ownership of one of them (It's a multiplayer VR game). Now we need to also assign ownership to all touching items to get local physics to look nice.
The naive brute force way would be to use OnCollisionStay and do the ownership check every frame. But its naive and increased frame time.
There must be a way of "resetting" the objects collision when taking ownership, so the OnCollisionEnter triggers again for all items it touches?

Maybe track all items currently touching an item in a list.
Add new item in OnCollisionEnter.
Remove item in OnCollisionExit.
Then you can loop thorugh all currently touching items to change the owner

Related

Does changing multiple sprites on a single animation affect performance?

I have been building a 2D sprite based game for which I want to have the player be able to customize their equipment. This means that although I am fine with drawing the content, I'd need to ensure animations in the game run fine on top of each other. For this, I have been preparing a game object with several children to account for the equipment:
Each of the children runs a single animation and should have to follow the player, which I accomplish by using transform.localPosition = Vector2.zero; on the Update of the script each takes, so they hook to the parent's pivot and follow the player. While this has worked for the most part, there are moments in which all of the objects are not synchronized and as such sometimes the parent object (the body) is seen where it shouldn't since the other game objects should render on top:
Aside from that, to make it easy for the children to follow the parent position I had made sprites which are all the same size, which risks me having to load a lot of transparent space per sprite.
Another problem that I just noticed as I'm trying to address the issue of loading too many useless pixels involves the positioning with other objects such as the Sword game object, which doesn't follow the player fully if I use sprites that are not perfect squares (see this question for details How to align sprites of smaller sizes to a moving gameobject sprite? and this one Sibling sprite doesn't appear to follow main GameObject sprite even when transform.position updates)
I tried to fix this by making the Sword a child of the Hero, but even then changing the position through a function that sets values to add on to transform values of the sword game object only change the position of it relative to the initial value. I attempted changing the pivot of the sword sprites to a custom value to guess where the center of it would align with the main game object and appear in the right position, but even that doesn't seem to work.
I'm kind of getting tired with my current process, as I have to rely on several animations for each of the game objects, both parent and children, so that these obey to different layers in a single animator (or in the case of the sword, a separate animator), all to ensure there is some synchronization that doesn't always occur:
I really don't mind the web that is turning out in what I'm doing, but the fact that I have to repeat it across multiple layers with no real guarantee that all the objects would appear right on top of each other due to the fact of having multiple animations playing, and loading multiple sprites with empty space is becoming more of a chore than enjoyment.
So I think I came up with a possible solution: If I could make a single animation for the whole equipment used at any given point (whether only wearing pants or wearing full equipment), then having this single animation could guarantee synchronization across parent and children without the need for animator layers or special functions to update position or worrying about pivots or square sprites if I can set the position of non-square sprites in the animation, with the downside that I would need to account for every single animation for each possible equipment variation (so if I had even 3 of each sword, pants, boots, etc. that would mean 3^6 animations) and make a more complex web of animator states. The only thing I'd be worried about in this case, however, would be the performance, if having too many animations for a player would affect how fast these load. But at the benefit of eliminating the other problems mentioned, my question boils down to this:
Is it better to have a single game object with animations that change multiple sprites across children game objects and a single animator that chooses states based on multiple variables, or game objects with multiple animations that change a single sprite for each, and a single or multiple animators with multiple layers that choose states based on multiple variables?
There isn't really a set answer for something like this. It really just depends on how good your/the players computer is when playing the game. Sorry if this isn't what you wanted.

Creating a Platform in sprite-kit

How would I make a sprite so that an object would be able to move through the bottom of it, but bounce off the top? as opposed to just being bounced off from any contact.
You could just add a 'jump' state check for your sprite,and while he is moving up turn off collision checking for the platform on your sprite, then when falling down, reenable it. You may have to add a check for whether or not the sprite is inside a platform though, because the physics will get you if you enable it while inside.
You could create two nodes of the same width that are stacked on each other (therefore looking like one). Give their physicsBody a different categoryBitMask and contactTestBitMask to differentiate them.
Now check for collisions and buffer them in a simple list. If you now handle the collisions just check which collision occured first: The collision with the upper or lower node? Then you know whether you need to add force (i.e., bounce) that object or not. :)

Object Poolers + Colliders + Animator, how do you do it?

Just ran into a serious performance problem.
I use object pooler script which loads all assets, sets them to inactive and returns an object from the pool whenever a script asks.
If object goes beyond screen, it is set to inactive and goes back to the pool.
The other script takes an object from the pool, sets its position and activates it. Now the problems:
1) If the object has a collider, it's expensive to move it via transform, but I cannot move it via rigidbody while it is inactive. I was thinking to leave the objects active, but wouldn't that be harsh on resources? How would you go about that?
2) Some objects have spritesheet animation and whenever they get reactived, the animator has to reinitialize and it creates spikes in the profiler. Could you recommend anything?
[Edit] The game was running smoothly until I added these new objects with colliders and animators. Now it's slowing down everytime these objects are activated.
Profiler shows that it's mainly due to these two problems: colliders changed and animation initializing.
One of the main problems was that object pooler created all objects with colliders in the same place and colliders were going crazy. Don't forget to set up your collision matrix, it definitely decreased the CPU load a lot.

Removing a fixture on body (cocos2d/box2d)

I have several balls bouncing around the screen, each with its own body and can bounce off one another. The user can pick up a ball and drag it around the screen.
I'd like to "kill" the selected ball's so that its temporarily removed from the space as long as the user is touching down on it. So other balls will bounce through it as if it weren't there. Upon release, the ball will regain its physical properties and can resume being bounced around.
How should I execute this? Should I remove the body entirely and re-create it upon touchEnded? Any ideas or help appreciated. Thanks
I believe you can't add/remove fixtures to an existing body. The way to achieve what you want is to disable the body (set it's active state to false). You could also change it's collision filter/mask so that it won't collide with certain game objects, but still collides with world boundaries for example.
You can wrap your Box2D body in a custom class that will keep the definition (bodyDef and fixtureDef are reusable) then you can :
destroy / recreate the fixture at position 0, 0 on the existing body moved by the touch
OR destroy / recreate the entire body at the touch position
I believe managing fixtures is better for performance.

Targeting all Subclasses and Sprites (cocos2d iPhone)

Ok, I have this main class called Enemy, and inside it I have subclasses of different enemies (ie ZombieEnemy). I need a way to target all sprites/subclasses of Enemy. Ie, for collision detection, I need a way to see if ALL Enemy's are 'dead' to end the Level.
Thanks
There are plenty of ways to do this. One is to add a method to your Enemy class like -(BOOL)isEnemy that simply returns YES. (That'd actually be more useful if Enemy has a superclass that you can customize, like GameObject. Implement -isEnemy in that class to return NO. Otherwise, you won't know if you can call -isEnemy on a given object.) Subclasses will automatically inherit this method. Alternately, you could test the class of each object using -isKindOfClass:. Or, since you're the one creating enemies, you could certainly keep a list of all active enemies. This is probably the best plan if you have lots of objects on the screen, only some of which are Enemy objects.
Deciding when all enemies are dead is something you probably want to do very often. It might make sense to keep a list of live enemies. When an enemy dies, remove it from the list. You can quickly test whether the player has successfully cleared the level by checking the length of the live enemies list. If it's greater than 0, there's more work to do.