Some thoughts on the physics.
Attempting to move this discussion from an email chain. Please excuse the crappy formatting:
Viktor:
I had a look a the latest version of the physics, I know it’s all a work in progress so things may change or are currently being updated. Some thoughts/suggestions:
I cannot find a way to get the area of a physics body (there used to be a property). This seems to be an important feature if you want to create different shapes with the same density – which is a very common use case. Minor detail: Enumerations should use NS_ENUM (or the Appledoc is unhappy). Also, enumerations and structs should use capital letters, e.g. CCPhysicsBodyType. It would be very useful to be able to mix actions with physics. To support that, the actions on nodes with physics bodies would need to be run at the fixed time step. (SpriteKit supports this feature) Is there a need to have kinetic vs static bodies. It would be really cool if this could be handled internally, say if a body hasn’t moved the last time step it is automatically changed to a static type and if you move it it switches to a kinetic type. Would case less confusion for users and be easier to use. (Again, SpriteKit supports this)
Vik
Martin:
I agree with mixing actions, in my own game (v2) it would have been quite handy to occasionally use actions to overwrite the physics movement in CCPhysicsSprite. A dynamic body that becomes inactive will go to sleep anyway?
Me:
- Area: The reason why I took this out is that it’s impossible to know the area of a shape/body until they are added to a physics node. Secondly, I haven’t decided if bodies should be rescaled/skewed when it’s transform changes in a non-rigid way. (Unity does this, and there are annoying downsides, SpriteKit doesn’t do it)
- NS_ENUM: check.
- Actions: There was some discussion about this before. Basically I think we need to create separate actions for physics enabled things. The action needs to know to update the velocity of a body, and realistically it’s only feasible to apply to kinematic bodies. Actions and physics together is a lovely idea, but I’m not convinced that it can ever work quite like people want them to.
SpriteKit doesn’t have a fixed timestep at all, which is very problematic from a physics point of view. Their implementation is to just run everything on the same variable timestep. I suspect that Sprite Kit also doesn’t update the velocity of bodies that it moves, and simply changes their positions. If true, that is exactly the sort of thing that we need to avoid. It’s a spectacular way to break physics.
- Kinematic bodies: Collision shapes attached to static bodies are stored differently than shapes attached to dynamic or kinematic bodies. While they can be moved, if you are doing it every frame it’s much more expensive to do so than using a kinematic body. On the other hand, having a kinematic body that never moves is also more expensive than using a static body. It’s a hint to the system what you are using the body for. If SpriteKit is allowing one to move static Box2D bodies around as if they are kinematic, then there are serious problems associated with that since they are defined to have a velocity of 0.
Viktor:
I’m not convinced having special actions for physics would be the best way to go (not super user friendly – and would be hard to integrate with SpriteBuilder animations), I can see the issue with the velocity though. Why not in the move/rotate actions check if there is a physics body attached to the node and if that is the case also apply the velocity. It should be possible to calculate the velocity from the last time step, but I guess there may be some trickiness to also update any ancestors. This, of course, would require actions that run on nodes with physics bodies to also run on the same fixed time steps.
SpriteKit doesn’t have a fixed timestep at all...
Their implementation may not be the best in this case (haven’t tried it), but they certainly thinks it’s important enough to make a big selling point out of it. Imo, even without applying velocities this seems to be useful in many cases. Maybe this should be a first step towards a better implementation? I understand the reasoning of having static and kinetic types, which totally makes sense. What I am curious to know is how costly it would be to move from one type to another. It would be pretty easy code-wise to check if a kinematic body hasn’t been moving from the last frame, and in that case turn it into a static body. Likewise if you move a static body, change the type to kinematic.
Actions: I agree that actions + physics seems like a bit of a killer feature, I also agree that it wouldn't be very user friendly to have separate actions. It's the best thing I can currently think of to work correctly from the physics standpoint (and I don't really like it). It might be possible to mask some of the extra complexity by making the physics versions private subclasses that are substituted as necessary. I'm also wondering if there is anything that would simplify this if the scheduler and action manager were merged into a single class since the action manager is so dependent anyway. I need a chance to think about this some more and play around with some implementations though.
As far as applying actions to dynamic bodies though, I'm not sure if we should even allow it. By overwriting the body's velocity each frame, it effectively is being driven by a nearly unstoppable force based on the mass and velocity of the body. As soon as that body collides with anything, bad things are going to happen. Anybody that wants to do that really wants a custom constraint(s) to control the body with. Unfortunately there isn't enough information to set that up automatically. In the case of a kinematic body, being driven by an unstoppable force is what should be expected since kinematic bodies are themselves unstoppable.
Kinematic Bodies: Changing the type of a body between static and non-static is at least as expensive as reindexing a static body. To detect if a kinematic body is moving or not would require keeping a list of them and caching their positions from frame to frame. Definitely cheaper to just reindex the static body in that case.
I wouldn't really be opposed to removing kinematic bodies from CCPhysics. Their usage really isn't that common that it would be a performance problem for many people if automatically reindexed static bodies were used instead.
Actions: If doing this, we should definitely try to hide the complexity from the user. A private sub class could be a way to go, but what would the disadvantages be of adding a special case to CCActionMoveXX and CCActionRotateXX?
Regarding applying the actions: I think this probably only would make sense for static/kinematic bodies, maybe throw an assert if it is applied to a dynamic body or silently turn the body into a static one while the action is running?
Kinematic Bodies: Detecting if a body has moved could be done by setting a flag when the body moves, no caching of positions are required. But if you think it will be less efficient than just having reindexable static bodies, my vote goes towards that.
I am not really sure what SK does, or how well it works, but I don't think we should base our design decisions on their marketing material. Having done quite a bit of chipmunk physics in the past, there is no doubt in my mind, that if the physics is not based on fixed time steps, we can just as well stop bothering implementing it. This is an absolute showstopper. Physics without fixed time step, is just a gimmick.
Also, I am not sure I follow the static /cinematic discussion? Static bodies are specifically placed, because the user never ever wants them to move. Otherwise he places a cinematic body. I personally think that makes a lot of sense, so I am uncertain why there suddenly is a need to change that?
As I have said before, as chipmunk is not a continuos collision engine ( nor do I think it should be ), I would personally like to see some functionality for predicting collisions in stead.
Agree with Lars on this, my first question regarding auto magical static/non-static. Is it actually a problem that needs to be solved as it will cause problems for so many people or will have a great positive gain for people?
Or is it just an exercise in trying to be more like SpriteKit which I don't agree is always necessary.
One bit I did like the sound of under SpriteKit docs was
"You can lock a static volume in place until the player performs the task that unlocks it. Then, change the body to a dynamic volume."
I liked the sound of that for example in my own game, if it featured a large map, areas could idle and then auto active into dynamic. However tbh even with mid sized maps performance was never really an issue with Chipmunk.
On 15 November 2013 11:21, Birkemose [email protected] wrote:
I am not really sure what SK does, or how well it works, but I don't think we should base our design decisions on they marketing material. Having done quite a bit of chipmunk physics in the past, there is no doubt in my mind, that if the physics is not based on fixed time steps, we can just as well stop bothering implementing it. This is an absolute showstopper. Physics without fixed time step, is just a gimmick.
Also, I am not sure I follow the static /cinematic discussion? Static bodies are specifically placed, because the user never ever wants them to move. Otherwise he places a cinematic body. I personally think that makes a lot of sense, so I am uncertain why there suddenly is a need to change that?
As I have said before, as chipmunk is not a continuos collision engine ( nor do I think it should be ), I would personally like to see some functionality for predicting collisions in stead.
— Reply to this email directly or view it on GitHubhttps://github.com/cocos2d/cocos2d-iphone/issues/381#issuecomment-28562542 .
Martin
I'm in complete agreement. Utterly.
Sent from my iPad
On Nov 15, 2013, at 7:21 PM, Birkemose [email protected] wrote:
I am not really sure what SK does, or how well it works, but I don't think we should base our design decisions on they marketing material. Having done quite a bit of chipmunk physics in the past, there is no doubt in my mind, that if the physics is not based on fixed time steps, we can just as well stop bothering implementing it. This is an absolute showstopper. Physics without fixed time step, is just a gimmick.
Also, I am not sure I follow the static /cinematic discussion? Static bodies are specifically placed, because the user never ever wants them to move. Otherwise he places a cinematic body. I personally think that makes a lot of sense, so I am uncertain why there suddenly is a need to change that?
As I have said before, as chipmunk is not a continuos collision engine ( nor do I think it should be ), I would personally like to see some functionality for predicting collisions in stead.
Reply to this email directly or view it on GitHub.
@Birkemose no one is arguing here that the physics shouldn't be on a fixed time step. The argument is that the actions that are run on nodes with physics bodies probably also should run on a fixed time step so that they can interact with the physics. Having actions and physics being able to interact would be a real killer feature. It's not just a marketing gimmick from Apple's part. Say you are making a platform game, and you have a moving platform, making it possible to animate the platform using actions would greatly simplify things.
I don't really have a very strong opinion on the static/dynamic/kinematic type and will be happy with any outcome of this discussion. My thinking is that as the static and kinematic types behave the same (I know implementation is different), it is somewhat confusing that they are named differently. If the correct type could be selected automatically, that would simplify things. If the performance difference is very small between the types, maybe having different types isn't necessary either.
Being able to switch between dynamic and static types would be very useful too I think! Is this already supported?
My bad. Sorry if I came across as if we are working on non-fixed time steps.
Well, the part about moving platforms, would be a real killer feature. I can see that. It is not really clear to me, from what Scott writes, if it would be possible to apply actions to "non kinematic" bodies? Sort of just like a collision shape you could move around. Or something. That would solve the platform needs? I guess though, that if bouncyness and friction ware to be calculated for such a "collision only shape", that it would still require a fixed time step?
As it is now, actions are just executed each frame. Wouldn't it be possible to change that, so that actions are executed either in update: or in updateFixed: ( if available ) on the actual node?. It would require [super update:] to be called if overridden, but it would also automatically make actions fixed time step, if a fixedUpdate exists.
Sounds like there is some confusion about static, dynamic, and kinematic bodies. Chipmunk, Unity (PhysX), and just about everything does share terminology on this:
Static bodies are for things that don't move: Terrain.
Dynamic objects will move freely: crates that can be pushed around.
Kinematic objects are physical objects that have their motion driven by code: A platform that moves back and forth.
I think this is a pretty short and sensical description that most users will be able to understand.
Now the errata you've all touched on: Most engines will allow you to move static bodies. This is generally just a strict setPosition, and under the hood, some list of static geometry is recalculated. If you want objects interacting with this to move nicely, you probably shouldn't be using a static body.
If you want something standing on a moving platform to move smoothly, the platform should be a kinematic body, and you should be supplying velocity information to the platform, so the engine can easily make sure things resting or touching the moving platform move appropriately.
So everybody is in agreement that actions on physics nodes is a good feature to have. Does everybody think it's a must have feature for 3.0.0?
Right now, actions on physics nodes should work (poorly) since actions just set the position property. They should even work approximately as well (or poorly) as they do in SpriteKit (see below), and exactly as well (or poorly) as moving a body from a non-fixed update method in Unity. If we really wanted to claim Cocos2D supports this as well as the competition, we can do that right now. I would honestly be embarrassed to call it a final solution though.
SpriteKit's implementation of this is definitely not something to aspire to. I have a hard time believing that it's behavior is actually useful to anyone. It doesn't expose kinematic bodies at all. Say you use a static body to make a horizontally moving platform. The action will change the position but not velocity of the platform and it will slide right out from under an object no matter what the friction is set to. If the platform moves up, it will mash objects into it's surface, only displacing them with what are known as pseudo-velocities. If the platform moves downward, objects will appear to bounce off of it's surface since they will accelrate, collide/stop, repeat. Applying actions to dynamic bodies has the same problems I described above.
I foresee two problems with actions/physics that need to be addressed for a really well working solution.
- Rigid body interpolation/extrapolation is one of the most important non-critical features I had planned for CCPhysics. It works without it, but would be a lot better with it. Actions are going to need to work alongside this feature.
- Actions currently operate exclusively on the rough, variable time step like the scheduler used to. The biggest downside of this is that the durations of actions are very inaccurate. I feel like a lot of the implementation would be quite easy if the scheduler and action manager were combined into a single entity.
If properly working physics/actions could delayed to be a 3.1 feature, I'd be much happier. That way there is more time to consider the issues involved.
About kinematic bodies. It seems even amongst us there is some confusion over what they are exactly. Maybe that is reason enough to try and blend the two body types together in the CCPhysics API. I'm sort of leaning towards leaving kinematic bodies out of the 3.0 release. Then we have more time to think about what to do with them. They could later be added back trivially or implemented as a blended static/kinematic type.
Personally for me it's not a show stopper if it's not implemented in 3.0 from day one. Although I'm presuming that 3.1 will not be far away ;)
3.0 is the new foundation and I would rather time was spent ensuring that everything is solid, especially the foundation physics before adding 'nice to have' functionality on top.
If a unified refactored scheduler/action manager would help with the next steps in Physics then that sounds like a good idea to do now, while musing 3.1.
I vote that we get the foundation right, before we add actions and kinematic bodies.
I've been playing around with actions on physics nodes, how is velocity calculated when applying for example CCMoveTo. I was comparing moving a node with an action vs applying force to achieve the same effective movement.
When you apply force the effect is like a bowling ball smashing other bodies which is nice however when moved using CCMoveTo it barely scratches other bodies.
I'm just thinking about these situation from the end user perspective, I'm my example I'm only using physics for collision detection so it doesn't matter that much however just thought I would mention it.
This was mostly covered a few comments up: https://github.com/cocos2d/cocos2d-iphone/issues/381#issuecomment-28714133
CCMoveTo is changing the position of the node each frame, and in turn changing the position of the body each frame. CCMoveTo doesn't know about changing the velocity and that's a problem. The effect is that the object is being teleported instead of moved each frame. When it teleports into another object, they sort of "mush" apart from each other. It currently works about as well (or poorly) as SpriteKit and Unity when you attempt to do the same thing.
The correct thing to do for kinematic bodies is to update both their position and velocity in the fixed update step. Since they have infinite mass and are unstoppable, this is a reasonable thing to do. For dynamic bodies, it's more complicated. The closest thing to "correct" is to use a force limited constraint - such as a pivot joint - to request the object to move to a certain position while respecting a reasonable maximum force. Setting the velocity of a dynamic body every frame can be problematic since it can easily cause huge forces to be applied in circumstances where they would be unexpected. Getting hit by a 50 mile an hour leaf wouldn't hurt at all. It would just stop since it contains so little energy. Getting hit by a 50 mile an hour leaf attached to a truck...
So there are two problems that need to be solved in the near future to make this work better than the competition:
- Make actions work together properly with fixed updates since right now they only work on regular updates.
- Figure out something reasonable to do with actions and dynamic bodies.
This is one area where Sprite Kit's physics are interesting... they've incorporated traditional animation with physics based animation.
At the cost of a fixed update cycle.
That's far to expensive a cost for the facility. IMNSHO.
On Mon, Jan 6, 2014 at 9:48 AM, slembcke [email protected] wrote:
This was mostly covered a few comments up: #381 (comment)https://github.com/cocos2d/cocos2d-iphone/issues/381#issuecomment-28714133
CCMoveTo is changing the position of the node each frame, and in turn changing the position of the body each frame. CCMoveTo doesn't know about changing the velocity and that's a problem. The effect is that the object is being teleported instead of moved each frame. When it teleports into another object, they sort of "mush" apart from each other. It currently works about as well (or poorly) as SpriteKit and Unity when you attempt to do the same thing.
The correct thing to do for kinematic bodies is to update both their position and velocity in the fixed update step. Since they have infinite mass and are unstoppable, this is a reasonable thing to do. For dynamic bodies, it's more complicated. The closest thing to "correct" is to use a force limited constraint - such as a pivot joint - to request the object to move to a certain position while respecting a reasonable maximum force. Setting the velocity of a dynamic body every frame can be problematic since it can easily cause huge forces to be applied in circumstances where they would be unexpected. Getting hit by a 50 mile an hour leaf wouldn't hurt at all. It would just stop since it contains so little energy. Getting hit by a 50 mile an hour leaf attached to a truck...
So there are two problems that need to be solved in the near future to make this work better than the competition:
- Make actions work together properly with fixed updates since right now they only work on regular updates.
- Figure out something reasonable to do with actions and dynamic bodies.
— Reply to this email directly or view it on GitHubhttps://github.com/cocos2d/cocos2d-iphone/issues/381#issuecomment-31669193 .
So here is a little movie of what I'm talking about as it works now in Cocos v3 and SpriteKit. Unity works a little differently since it seems to use penalty forces instead of psuedo velocities, but still tends to break when you do this.

The platforms are static bodies that are moved using actions. Only their position is updated so weird things happen in both cases. Once actions know about setting velocities too, then it will work as expected, and will be pretty awesome for a lot of things.
I have been toying a lot with physics the last week or two, and my general feeling is, that they are very stable. Placing objects upon each other, has always been a bit of a struggle. Right now, I can stacks spheres, and they will keep stacked, even at fluctuating frame rates. That is pretty impressive.
I'm going to close this issue since it's covered in #479.