I'm trying to add water to my game. Except for a different background color, there isn't much to it.
However, I'd like the player-sprite
to float on top of it (or halfway in it). If the player just walks into the water from below, I'd like him to float to the top. If he falls down, I'd like him to slowly change direction and float back up.
I tried making the gravity negative when he's in the water, but this gives me some slightly unwanted effects. For example as he (the player) surfaces, the normal gravity will push him back down, the water will push him up, and so on. Ultimately the player will be "bouncing" in the water, being pushed from one end to another. I'd like him to calmly remain on top of the water when he surfaces. How can I achieve this?
Here's the code I have in my update-loop
:
SKNode *backgroundNodeAtPoint = [_bgLayer nodeAtPoint:_ball.position];
if ([backgroundNodeAtPoint.name isEqualToString:@"WATER"]) {
self.physicsWorld.gravity = CGVectorMake(self.physicsWorld.gravity.dx, 2);
} else {
if (self.physicsWorld.gravity.dy != -4) {
self.physicsWorld.gravity = CGVectorMake(self.physicsWorld.gravity.dx, -4);
}
}
Basically this changes my gravity to 2
when the player is in the water, and otherwise changes it to -4
unless it's already -4
.
Thanks!
There are three possible options I believe you have with regards to simulating water.
1) As mentioned in the comments you could try to use SKFieldNode (iOS 8+). But from personal experience the field node didn't really do it for me because you don't get much control over your simulation with it unless you heavily customize it, in which case you might as well just do your own calculations from scratch and reduce complexity.
2) You could adjust the linear and rotational damping of your sprite when inside the water. In fact, even apple mentions this in the quote from their documentation. However this won't give you buoyancy.
3) Perform the calculations yourself. In the update method, check when the body enters you "water" and when it does you can calculate viscosity and/or buoyancy and adjust the velocity of your node accordingly. This in my opinion is the best option but also the more difficult.
Edit: I just wrote a quick example of option 3 in Swift. I think it's what you are looking for. I added factor constants on the top so you can adjust it to get exactly what you want. The motion is applied dynamically so it won't interfere with you current velocity (i.e. you can control your character while in the water). Below is the code for the scene and a gif as well. Keep in mind that the delta time is assumed to be 60 frames a second (1/60) and there is no velocity clamping. These are features you may or may not want depending on your game.
Swift
Objective-C
When your player makes contact with the water, push him up until he is no longer in contact with the water. At this point, modify the player's contact bit mask to collide with the water thus "making him walk on water".
Alternately, you can trigger the walk on water contact bit mask modification by a strategically placed invisible node instead waiting for water contact to occur.
To revert the player's contact bit mask back to normal use another predetermined contact the player will make, such as land or an invisible node, as the trigger.