0
\$\begingroup\$

I want to make a 2D soccer game on iOS, I started with SpriteKit and got the ball physics working. Now, I could make the goal simple and static, but I'd love to make it make it elastic like a real one when the ball hits the net. I started creating the goal with the post, two ropes (on the top and rear side). The ropes are basically like the ones in https://github.com/DigitalBuckeye/SKSwiftRopeDemo.

My problem is, when I hit the ball fast enough against the ropes, it will pass through them (even with usesPreciseCollisionDetection enabled).

I couldn't find any 2D sample for a good goal net on the web, tried searching in the box2d and chipmunk communities.

I wonder if I'll have to use a 3D engine for that.

\$\endgroup\$
  • \$\begingroup\$ It's not connected with question, but it's a tip: You should add .DS_Store files to .gitignore. \$\endgroup\$ – Marqin Jan 13 '16 at 14:19
  • \$\begingroup\$ Thanks Marqin, but that github repo is not mine ;-) \$\endgroup\$ – bjoern Jan 13 '16 at 14:20
1
\$\begingroup\$

My problem is, when I hit the ball fast enough against the ropes, it will pass through them (even with usesPreciseCollisionDetection enabled).

This is probably because it's moving so fast that:

  • Frame n: Ball is still in front of net, not colliding with net.
  • Frame n+1: Ball is on the different side of net, not colliding with net.

This problem has it's own question, so I'll just summarize possible options:

  1. Limit ball speed.
  2. Check in every frame if ball had passed trough net.
  3. Do collision test in each frame ( before moving ball ) with some raycast to check if path in front of ball is clear ( no obstacles, eg. net ).
  4. "Create a minimal bounding box that contains [ball position in] Frame n and Frame n+1 and see if the goal intersects [with this box]" - as suggested in comment by Erno de Weerd
\$\endgroup\$
  • 1
    \$\begingroup\$ Or create a minimal bounding box that contains Frame n and Frame n+1 and see if the goal intersects \$\endgroup\$ – Erno de Weerd Jan 13 '16 at 14:39
  • \$\begingroup\$ That suggestion may be a broad phase check. A bounding box can be created where the ball in frame n is near the edge of the net, n+1 behind and left of the net. The bounding box will intersect, however the path of the ball just missed the goal in reality. \$\endgroup\$ – Felsir Jan 13 '16 at 14:58
  • \$\begingroup\$ ^ Only when ball can move on a curve. \$\endgroup\$ – Marqin Jan 13 '16 at 15:01
  • \$\begingroup\$ Well if you make the boundingbox rotate you're correct that it will always intersect. However in a 2D game boundingboxes are usually axis aligned. In that event you can draw a box that intersects without the path actually hitting the goal. If you are going to rotate the boundingbox you are better off doing a line intersection of the ball path with the goal rope, which is the easiest solution to the OP's question. You get the intersectionpoint as a result to keep the ball inside the goal and have the net react to the impact at the correct spot. \$\endgroup\$ – Felsir Jan 13 '16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.