I have found out thanks to this article: http://blog.element84.com/comparing-sprite-kit-physics-to-direct-box2d.html and personal experience that Sprite Kit is not deterministic when using physics simulations. However I was wondering if the collision logic actually deterministic when handling the node's position in a deterministic way. i.e. Repeatability works fine but my position handling.
Thanks!
UPDATE: Added more detail
In SpriteKit, physics simulations don't seem to be deterministic since they are evaluated in the game loop and depending on the frame rate of the device they can be executed at different rates. My question, is collision detection (such as the didBeginContact method) for physics bodies independent from the loop and called right after the position of the node has changed. I am trying to use only the collision properties from Sprite Kit to achieve repeatability in 2 instances that might perform at different frame rates.
Just in case this could help someone, I just found out that all collisions happen in the Sprite Kit loop, therefore they are not repeatable nor deterministic for that matter.
Related
I'm working for a small bouncing ball game using the SpriteKit physics engine. My problem is:
When I apply a huge impulse on the bouncing ball to have it fall on ground fast, sometimes it may pass through the ground (very thin, height=2).
I find this in the Apple document, but it doesn't work.
Specify High Precision Collisions for Small or Fast-Moving Objects
When Sprite Kit performs collision detection, it first determines the locations of all of the physics bodies in the scene. Then it determines whether collisions or contacts occurred. This computational method is fast, but can sometimes result in missed collisions. A small body might move so fast that it completely passes through another physics body without ever having a frame of animation where the two touch each other.
If you have physics bodies that must collide, you can hint to Sprite Kit to use a more precise collision model to check for interactions. This model is more expensive, so it should be used sparingly. When either body uses precise collisions, multiple movement positions are contacted and tested to ensure that all contacts are detected.
ship.physicsBody.usesPreciseCollisionDetection = YES;
You can set your sprite's node.physicsBody.usesPreciseCollisionDetection = YES; but this will not guarantee a collision flag all the time as your sprite's velocity could simply be just too high.
You should apply a speed limit to your nodes as to prevent them from going too fast. Something like this:
if(node.physicsBody.velocity.dx > 200)
node.physicsBody.velocity = CGVectorMake(200, node.physicsBody.velocity.dy);
The above code example will limit the right movement, dx, of your node to 200 while keeping the dy (up/down) velocity as is.
I have used cocos2d-x-3.0 built-in physics for my game.
My problem is that my physics bodies are passing through physics boundaries, around the physics world. It is observed when physics body size is small and velocity is high.
I used box2d for my past native (cocos2d-iPhone) games, there, I solved this problem by enabling isBullet property of physics bodies but here, chipmunk does not support this (continuous collision).
Is there a way to fix it? or should I leave built-in physics engine and implement box2d by my own?
Now you can try to use PhysicsWorld::setSubsteps
One physics update will be divided into several substeps to increase its accuracy. Or PhysicsWorld::setUpdateRate Set it higher can improve performance, set it lower can improve accuracy of physics world simulation. I hope this helps:)
I'm trying to set up some elastic collisions using Sprite Kit. There is an issue with the case of multiple objects resting near each other as I asked in Sprite Kit Physics Collision Issue
I am confused on the timing of the situation for a collision.
I've tried to set dynamic to NO in -didBeginContact: delegate method, calculate final speeds, then in -didEndContact: set dynamic to YES and then set the velocities correctly.
The reason I want it to be dynamic outside of the collision is because I want friction/gravity etc to be available. What is wrong with the order/logic? I looked at -didSimulatePhysics method, but it didn't seem like the way to go.
I'm trying to set up some elastic collisions using Sprite Kit
I think you're misunderstanding the physics in play here. An elastic collision is an oversimplification of a collision. In reality, no collision results in a perfect transfer of energy. I've explained the physics here in an answer to the related question you linked to.
I suspect reading through that you will see that an "elastic" collision can be done in SpriteKit, but most people don't realize you need space between the objects. You shouldn't need to do your own physics calculations.
I'm not sure if this is exactly what you are trying to do, but if I wanted to compute results of collisions without the help of SpriteKit while leaving all other physics to SpriteKit, I would do the following:
Leave the dynamic property always set to YES.
Set the categoryBitMask property for each kind of object.
Set the collisionBitMask property for an object to the disjunction of all categoryBitMasks for which you want to retain automatic collision calculations (e.g., walls, but not other objects), or just to zero, if you want to handle all collisions manually.
Set the contactTestBitMask property for an object to anything for which you want to calculate collisions manually.
Compute and set all resulting velocities in didBeginContact.
(In short, exclude your objects from their own collisionBitMask.)
This link might be helpful: Working with Collisions and Contacts
Use applyImpulse instead of setVelocity. You want to be using impulses or forces to ensure the physics engine can warm up properly. Using setVelocity will yield unpredictable results for very close or overlapping objects.
Ensure there is some distance between the objects and that they don't overlap.
Setup the collision bit masks correctly.
This should all work perfectly.
I am a long time user of Stackoverflow but first post.
My question is seemingly simple, is there a way to make particles from an emitter interact with the physics sprites in the scene? (For example, if I am using a particle for rain, and I want it to bounce or bumpy off a sprite of a man with an umbrella. There must be a way, but I don't see a lot of documentation on adding physics to individual particles. Any ideas?
Thanks!
No. There is no way to make SpriteKit's built in particles interact with physics bodies. Every particle property you can control is a property of SKEmitterNode, and it has no properties for setting physics behavior for particles.
The fact is that particles are designed to be very light-weight so that you can have thousands of them on any hardware supported by SpriteKit. Physics simulation is not light-weight.
There is LiquidFun, which is a Box2D extension that simulate the physics of a particle system. This engine is the basis for Apple Spritekit physics engine and you can use it in your game but you have to tweek it a little bit to make it run. There are a lot of tutorials of how to use it in an ios project. I am confident that Apple will have more features added to Spritekit in the future that make the particle system respond to physics.
You could use a SKField to simulate gravity and then another field on your umbrella to repulse it.
thanks for taking the time to read my question.
I'm writing a 2d top-down shooter game. It is currently using Box2d as a physics engine. The thing is, it isn't really using Box2d to it's fullest potential, just for collision detection and the underlying velocity/rotation update loop. Any plans to add real physics would simply be eye-candy, not a game changer.
Now I chose Box2d because I went through 2 other physics engines, and they just couldn't handle the types of collisions I'm detecting. I'm creating several 'bullets' with very high velocities, and I do not want them to be instant hits on their targets. JigLib and Flixel both had the same problem - bullets were not overlapping enemies at the time of the frame update, and thus were not detected as collisions (i.e. the bullets passed through enemies because they moved to fast).
I moved to Box2d because of it's iterative collision sampling, as well as the SetAsBullet method on bodies. And it works great! But now Box2d is giving me troubles too - generating several bullets per second, or at the same time, is severely lowering my fps.
So I removed Box2d to confirm that it was not a rendering limitation... added my own velocity/rotation system, and I can fire hundreds of bullets per second. Great! But its lacking any sort of collision detection.
So the questions:
1) Should I write my own iterative collision engine?
2) Should I give Box2d a try again, perhaps with some tweaks to make adding new bodies faster?
3) Is there some other alternative, maybe a lightweight physics engine that specializes in this?
4) Do you know of any other techniques or design patterns that could be of use?
Thanks so much for your help!
Edit: I should note, there are not just bullets, but larger, slower projectiles as well. I considered ray casting a line segment to the projectile's previous position, and catching intersections, but that won't work for the larger objects :(
It depends on how complex your situation can become, If you are good at math and physics you can rollout a fast engine that can handle simple collisions more faster than you can learn using box2d, but why should anyone invent the bycicle if there are plenty of them already invented so choose one you like a try using it, i recommend using box2d