-1

xcode 5 iOS7 sprite kit.

My wish is to make a sprite that has its own gravity. Like a planet. If another sprite comes within a certain range of it, it will slowly pull the other sprite closer.

I have two sprites. One moving and one stationary. When the moving sprite gets in a given distance of the stationary sprite the stationary sprite gravity should slowly pull the other sprite towards it. This way the moving sprite would change its path in a soft curve.

My idea would be to calculate the distance from the stationary object to any other object and if close enough start pulling and if the moving object gets out of range ageing, then stop pulling. Would probably need to research some vector calculation. Thats my immediate thoughts.

Is this possible and how? Does it already exist?

  • Your question is too broad. What have you tried so far? What code did you use? – sangony May 22 '14 at 11:17
  • well he is asking is it possible ... so you can infer the answer to your quesion – John Nicholas May 22 '14 at 11:53
  • http://meta.stackoverflow.com/questions/254262/before-you-post-your-next-question?rq=1 <-- sangony this will probably get locked as dup / low quality – John Nicholas May 22 '14 at 11:56
  • @JohnNicholas - yes, he is asking is it possible but also asking how. Which is another example of "Hey I have a great idea but no clue on how to do it. Someone provide me with all of the code please." – sangony May 22 '14 at 13:22
  • So, do you want to write your own engine or not? – Ali Naci Erdem May 23 '14 at 09:50

3 Answers3

1

A friend of mine did this for his physics dissertation. multibody gravity simulation. So yeah you can but you need to be willing to learn some maths. Apparently there is a clever optimisation to make it run decently nlog(n) rather than n^2). you probably want to ask this on the physics version of stack overflow to get a good answer. I have the code at home ... will post it later but you will want an explanation - i used it in an xna app. Its badass once you get it working - although if you want naturally orbiting objects then you will want to code them using parametric equations for easy and cool orbits. Simply because its hard to solve and with time even using double will result in some errors (the good implementations also work out the error and adjust - again will post later). But the real problem is solving for stable orbits. You then use the algorithm for free moving bodies such and player objects / npcs. Although solving accurate movement for npc in a changing field is v hard.

you want to look at this question: Jon Purdys answer is the one you want

multi body physics - gravity

and this one (which is linked from above) ...

https://gamedev.stackexchange.com/a/19404

Community
  • 1
  • 1
John Nicholas
  • 4,778
  • 4
  • 31
  • 50
  • I don't think user3631877 is looking for such an elaborate answer, but who knows? :) – Ali Naci Erdem May 22 '14 at 17:56
  • Thanks for the answer, i look forward to see the code. I was thinking i might need to calculate the distance between the two objects. And in a given distance begin the pull. Probably need to look at some vectors. – user3631877 May 23 '14 at 09:25
  • and then some ... you should probably read the content of that link. Its all there. I couldn't find my c# code last night. Too many projects trying out crazy ideas. But if you don't understand matricies, fixed point numbers, calculating error on a float, barycenters or complexity of algorithms then you arn't going to get far. – John Nicholas May 23 '14 at 09:39
  • 1
    this is why i should stop answering questions like this. You point a guy to an answer and not even an upvote. – John Nicholas May 28 '14 at 08:51
0

There is not a dead-simple way of doing that in any platform as far as I know maybe except for some game engines/platforms that export for different platforms (cocos2d, construct 2 etc).

What you need is a physics engine whether you write one (which is a complicated but fun thing to do) or use an available one.

Luckily I've found a link describing the usage of the famous 2D physics engine "Box2D" on iOS.

This one explains how you can include Box2D in an openGL application (but you can apply this to other environments (sprite kit) I think altough I'm not an iOS guy);

http://www.iforce2d.net/b2dtut/setup-ios

Anyways, you now know what to look for...

Ali Naci Erdem
  • 435
  • 2
  • 12
0
  1. For iOS 8.0+ : you have SKFieldNode : radialGravityField()

  2. For iOS 8.0- : one solution could be : 3 to 9

  3. add your sprite ( planet ) with its physics
  4. add an invisible SKNode ( the gravity zone ) with its own physics, as a child of your sprite, but with a radius much more than your sprite's one
  5. you have a little explanation about invisible node here : https://developer.apple.com/documentation/spritekit/skphysicsworld
  6. both, your sprite and the invisible node are centered in a zero gravity world
  7. we look for contact and not collision ( set correctly your bit masks )
  8. when any object get in contact with the invisible node ( gravity zone ), first you remove any moving action or impulse from it and then we add to this object an SKAction to move it toward the center of your sprite ( planet )
  9. at second contact between this object and your sprite ( planet ), you remove all actions again from the object with a velocity = zero
hungry
  • 174
  • 8