Continuous collision in cocos2d-x 3.0 built-in physics (chipmunk) - ios

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:)

Related

Spritekit - applying an acceleration to a physics body

I'm working on a project where I have a node affected by gravity. I would like to be able to apply a vertical acceleration to this node without touching gravity. Pretty much like a hot air balloon that I would make go up or down while other objects are still normally affected by gravity.
balloon = Vehicle()
balloon.physicsBody = SKPhysicsBody(polygonFrom: path)
balloon.physicsBody?.affectedByGravity = true
I tried playing around with applyForce(_:) and impulse but what I'd like is a proper acceleration and not a punctual impulse to defy gravity over time! Any suggestion?
What you're looking for are physics fields (SKFieldNode), in Sprite Kit.
These provide the ability to influence objects with general forces, like wind, lift, black hole style attraction etc.
You can read about them here.
The really important consideration of how they can work is in this quote:
Physics bodies with affectedByGravity set to false are still affected
by the gravity fields created by linearGravityField(withVector:) and
radialGravityField().
But what they forgot to also make clear(er) is that objects influenced by gravity are ALSO influenced by these fields.

How to get Constant Velocity for Spritekit Gravity

I am making a Tetris kind of game, where I want my blocks to fall at a constant velocity. I want to achieve it using SpriteKit Physics Words Gravity.
Problem is that gravity is necessarily acceleration and hence block start to move faster as they fall. I know I can turn gravity off and use actions to move the block but I wish to do it with gravity.
Is there a way to make the gravitational force in SpriteKit world a linear constant velocity?
Using the default physics you will not achieve what you are looking for.
In the definition of Gravity in Spite Kit is clear that is a acceleration, so is no possible to achieve linear speed with it.
A vector that specifies the gravitational acceleration applied to
physics bodies in the physics world.
But, you can have some workarounds to achieve the behavior you want.
Gravity Acceleration 0, and add speed to the falling object.
Limit the maximum Speed of an Object.
Do you own physics
I Think the best way to do it, with gravity working as default, is by limiting the maximum speed.
2 - Limit the maximum Speed
Consulting this post.
In didSimulatePhysics(), you can verify the speed of the object, and limit it.
Like this (Credits to #Andrew, in the original post)
- (void)didSimulatePhysics {
if (node.physicsBody.velocity.x < MAX_SPEED_X) {
node.physicsBody.velocity = CGVectorMake(MAX_SPEED_X, MAX_SPEED_Y);
}
}

Is collision detection in Sprite Kit deterministic when not using physics?

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.

Individual particles and physics in Sprite Kit

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.

Collision detect with iterative sampling - should I use an engine (box2d) or roll my own?

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

Resources