Sprite Kit's 'didBeginContact' function is not very accurate. What should I do?

*爱你&永不变心* 提交于 2019-11-29 15:18:11

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/23485232/sprite-kits-didbegincontact-function-is-not-very-accurate-what-should-i-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!