SceneKit physics issue with dynamic bodies - swift

Here is an interesting issue that i have been stuck on for a while and couldn't figure out. I'm playing around in SceneKit (using swift). Whenever or wherever i attempt to put objects with a dynamic physics body into a node hierarchy (i.e add them as a child to another node), the physics bodies separate from the models (sometimes the models just fly away etc). It always works perfectly if I set the bodies to static. Has anyone heard of such thing?
Edit:
Sorry if it was slightly unclear previously. Just to add some more information on the issue:
I create the parent node multiple times in a scene. The first time it is created everything is perfect, even objects with dynamic bodies. Now if i create another instance of the parent node further down the z axis for example, the dynamic physics bodies appear to be exactly in the right place but the 3d model not connected to the physics body. Sometimes the model is in sight but nowhere near the physics body or not to be seen at all. If it is visible, as soon as my main game object collides with the dynamic body, the physics body responds as expected but the model disappears instantly. So just to clarify, the dynamic physics body is behaving normally but my imported model that the dynamic body belongs to does not. This is only the case when my object is in a node hierarchy.
As far as setting them to not be affected by gravity, I actually do want them to be affected by it!
Thanks!
Sam

There is a difference between the location of SCNNode.presentation and the node itself. The physics engine will move the presentation. If you set properties on the SCNNode like rotation, position, etc it should reset the node and presentation to match or you could call reset* (not in front of docs right now to get the right name) to reset it manually if needed.
As Mago said, there is probably gravity moving your dynamic bodies. You can turn gravity off from the SceneView's physics engine property, or you can set the physics body's property to not be affected by gravity and see if that fixes it.
Given your description I'm not sure exactly what you observe since the physics body isn't visible just the geometry attached to the SCNNode. Perhaps a little more detail of what you observe that leads to the conclusion that "the physics bodies separate from the models"?

Related

issue with character controller collider in a c# unity 3d game

I'm having a peculiar issue with my character controller collider. in the image provided the controller SHOULD fall down the side of this cube object, yet it "floats" or "hangs" on the corner which isn't a detrimental problem, but is something I'd like to fix now rather than later, any ideas?
image of the controller statically floating on the edge of a cube object
I initially thought this issue may of been caused by incorrect properties within the controller, however after experimenting with the values the issue has not be resolved. I even tried ray casting to determine whether the controller is actively colliding with an object, but to no avail.
The problem is
it seems that the problem isn't directly on the collider but rather on the material you're using (which in this case, none)
Solution
from your project's asset, right-click and select create -> Physic material. From that physic Material, set the both Dynamic and Static friction to zero, then just draw that physic material to the game object. you can also adjust the friction depends on your needs
Character Controller has a skin width. Its an extra layer of collider which may cause this. Also check if the collider is within the capsule bounds. You can check out this article on Unity Character Controller for more details.

Unity physics scenes copy all colliders from main scene

I am trying to use Unity's physic scenes to perform some simulations outside of the main scene. That being said, I need all of the colliders from the main scene in the physics scenes. What is the easiest way to create a physics scene with all of the existing colliders from another scene? Do I have to loop through everything, use SceneManager.MoveGameObjectToScene(...) for every object then move them back at the end of the simulation? This doesn't seem scalable.
EDIT
I have a multiplayer game, when I get responses from the server I am taking their velocity and the servers position, then applying all local movement since that packet was sent via Physics2D.Simulate() to have the velocities correctly applied to the position. Now I need to do the same for the projectiles so I would need the map colliders as well as the player's. I was reading the best thing to do for physics sims is use physic scenes. If I call Physics2D.Simulate() in the main scene for more than 1 set of data (player movement vs projectiles) places I got a lot of screen jitter. Moving these simulations into separate scenes removes the jitter.
The only alternative I see is re-implementing Physics2D.Simulate() myself which seems like a bad (and complicated) idea.
Thanks.
I ended up only needing the collider and rigid body so I created an empty object in the physics scene with these 2 objects. Every time I needed to run the simulation, I would update the rigid body with the current players rigid body details then run the sim and apply the output velocity from the sim to the players rigid body again.

Adding Collision Detection to SKTileMapNode

What's the recommended way of adding collision detection to an SKTileMapNode for a side scroller?
Say I have a simple tilemap with just one single tile to use as the ground, how can I detect when my player sprite lands on one of the filled ground tiles?
I'm not entirely sure this will work, but it seems you could add a node to each of a SKTileMapNode's tiles and, based on whatever texture component is on it, give it a physics body appropriately shaped and setup for matching that texture.
Here is someone attempting something like this:
https://forums.developer.apple.com/thread/50043
You don't ask for it, but for greater granularity (eg a tile that's partially covered and needs a sloping physics floor element), it seems this would be the place to start:
https://developer.apple.com/reference/spritekit/sktiledefinition
But I can't find exactly how to know what part of a texture is on any given tile of a tile-map. There must be a way to do this, but I'm just not quite seeing it.

2D physics on game object made up of components

Say you want to let your users create a 2D airship (side view) made up of components. One component could be a floating balloon, another could be a storage room, etc.
And say you want this airship to have physics applied to it as a whole, with each component playing a part in this. For example, balloons would take away from its down force and other compartments would apply down force to it.
At the same time, the whole airship works as a whole (or at least the only physical separation would be between floater components and the others, which apply weight), so physics are applied to it as if it was one body.
Now, how can this be managed? How can I customize a game object via a script, giving it different components with different weights, and have it behave like it was a single premade object?
I'm sorry if the question is simple, but I'm only getting started with unity. Thank you very much!
TL;DR: Customizable airship gameobject with different components that have different weights. How to make it behave as a single physics entity and manage its different components?
I'm not sure that I completely understood the question :D
You mean having a game object, that has some child game objects, each with individual Collider and Rigidbody, and the Rigidbody physics applied to the parent game object? You can use joint colliders for this purpose such as HingeJoint2D, and set the parent's Rigidbody as ConnectedRigidBody.
(And instead of the word "component", use child or part or sth else to prevent ambiguity. 'Cuz the components in Unity3D are stuff like scripts, rigidbody, collider, etc. which are attached to game objects.)

Can I move dynamic physics bodies using SKAction when only contact detection is needed?

I am looking at tutorial where things are defined like this:
planes are sprites with dynamic physics bodies
plane moving is done with actions by following the path
there is contact detection between bullet and plane
bullet is sprite and it has physics body set to be static (which is little unusual in my opinion)
Here is the link to tutorial for more information.
Questions:
When we use actions to move physicsBodies is there a difference how we set body's dynamic property? Because bullet is static but still there is no problem for movement.
When we have situation like this, where we don't need collision detection, but just contact detection, and we can't move sprites (enemies) by applying forces or impulses, what is a good approach? Is this approach correct?
I think this is nice way, but I would like to be fully aware what is really happening when we use this method and are there any drawbacks or possible problems.
There are posts on SO that suggest we shouldn't use actions for moving dynamic physics bodies. I am aware that we can't use this approach in every case. For example this would not work for moving platform with other object on it, because there would be no correct physics simulation between body on the platform and platform moved by action. But in cases like from this tutorial, where we only need contact detection for object that can be moved only by actions (moved along path) I suppose it's not a problem ?
static means that the body isnt affected by physics. That doesnt mean it cant be manually repositioned or moved. So if something is set to static, it participates in the physics simulation, but it isnt affected by it. Think of a plane hitting a mountain. The plane is dynamic, the mountain is just going to sit there even though its participating in the physics. But you could still move that mountain around manually using a position or an action.
Not totally understanding your question, but I'll give it a shot.
You can move physicsBody's manually (using position property or actions), but you need to ask yourself why you're doing that. You typically don't want to do it because they're bypassing the physics simulation. If you're forcing it to move around, what's the point of using a physics body in the first place.. right?
But if your physicsbody is something like a powerup that is totally static, and you just want it to move around in a circle using an action then thats fine. You probably just want to use contact detection for the bullet, powerup, etc without actually moving it around using physics. Nothing wrong with that.