Question

Okay, here's the deal: I'm developing a 2D car game (similar to Hill Climb Racing) in Sprite Kit for iOS. I'm currently working on dust particles, which show up whenever a tire touches a surface that has this effect assigned to it. For this, I obviously need the functions 'didBeginContact' and 'didEndContact' so that I can turn the effect on/off depending on whether the tires are touching the ground or not. Here's what I want to do:

/* ---- very dirty pseudocode ---- */

didBeginContact {
    if (contact.bodyA is one of the wheels &&
        contact.bodyB is the surface that should make a dust effect) {
        engage dust effect
    }
}

didEndContact {
    if (contact.bodyA is one of the wheels &&
        contact.bodyB is the surface that should make a dust effect) {
        disable the dust effect
    }
}

This works fine for most of the cases. But here's the problem: If one or both of the wheels lift off the ground for a very very short period of time (let's say the vehicle bumps into something which results in a sudden stop or when driving over some bumps at speed) then the effect tends to stop, and even if the wheel is actually touching the surface, the effect does not show up until the wheel lifts off the ground and touches it again. I feel like Sprite Kit can't keep up with very fast moving things, and it's messing up these function calls if something sudden and fast happens. I did try this:

leftWheel.physicsBody.usesPreciseCollisionDetection   = YES;
rightWheel.physicsBody.usesPreciseCollisionDetection  = YES;

But it didn't help. The collision detection in Sprite Kit is still missing out some collisions that happen very fast.

Is there a solution to this? The problem is that Sprite Kit only calls this functions whenever a collision has begun or ended. So if it messes up one of the function calls, then it leaves the rest of the code believing that the wheel is not touching the ground. Is there a solution to check for collisions every frame? That would help a lot!

Here's an image visualizing the problem

I hope I described the problem clearly. Thanks for any help in advance!

Was it helpful?

Solution

You can use the allContactedBodies property of your sprite to check current contacts between your wheels and the ground. Polling this may not be as efficient as the event-driven method, but it may be more reliable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top