|
Title: Help with Chipmunk/Pymunk Planetary Physics Post by: NinthPower on November 06, 2013, 03:30:01 PM Warning: Length.
Language: Python A friend and I are starting development on a game based around the mechanic of herding sheep. Using Pymunk, a python wrapper for Chipmunk, I am trying to generate ~20 bodies with a circle shape, each representing a sheep in the herd. I was able to push them around well enough by creating a similar circle/body with a larger radius whose position (both for Pymunk and the Pygame image) is based on the position of my mouse. This represents the shepherd. You can watch a video of this concept I put up to my newly-made dev-log site here: http://studiotrex.wordpress.com/ (http://studiotrex.wordpress.com/) This is all great and super awesome and I was able to implement this in an hour or two. The problem is that it is hard to get a solid 'herding' feel to the circles representing the sheep. Changing my shepherd shape from a circle to something flatter will help, but I thought it would be awesome if I could get the sheep to move more as a group. I figured the best way to do this would be to treat each sheep object as a small 'planet' with a small gravitational pull, pulling on the other sheep around them. I think it can work but I'm having trouble implementing it. I found the 'Planet.c' example packaged with the Chipmunk examples, which synthesizes planetary gravity but I'm having trouble implementing it in Pymunk and applying this concept to 20 objects in my physical space. I figure I may be missing something in my translation from C to Python. Is my Newtonian equation off maybe? My question is: How do I define the origin of the gravitational force for each of my sheep!? Here is the Obj-C example: http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/Planet.c (http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/Planet.c) Here is a video of my implementation as it stands in the code below: http://www.youtube.com/watch?v=VLtOzWNb5ko&feature=youtu.be (http://www.youtube.com/watch?v=VLtOzWNb5ko&feature=youtu.be) Code: def planetoid_gravity(body, gravity, damping, dt): """synthesizes gravity based on object's own position""" p = Vec2d(body.position) sqdist = p.get_length_sqrd() g = (p * -gravity)/(sqdist * math.sqrt(sqdist)) pymunk.Body.update_velocity(body, g, damping, dt) ...later in the code # check the position of all the balls we created for ball in balls: # if the position is beyond a certain point (y=200 in # this case), add it to the list of balls to remove if ball.body.position.y < 200: balls_to_remove.append(ball) #Apply "planet" gravity planetoid_gravity(ball.body, 1000000000, 1.0, dt) # update the position of each ball and draw a blue circle p = to_pygame(ball.body.position) pygame.draw.circle(screen, THECOLORS["blue"], p, int(ball.radius), 2) In my code I am running this planetoid_gravity function each frame. Didn't seem to change if I called it when the balls were created. Sorry for the length and gross code, it's day two! This is just the core mechanic of the whole game and I'd like to figure it out before we devote more time to it. Thanks :gomez: ps- the gravity-force passed into the planetoid function is purposefully humungous so I can quickly tell if its working or not. Title: Re: Help with Chipmunk/Pymunk Planetary Physics Post by: Will Vale on November 06, 2013, 04:07:07 PM You appear to be applying gravity based on the position of the sheep relative to the origin:
Code: p = Vec2d(body.position) sqdist = p.get_length_sqrd() g = (p * -gravity)/(sqdist * math.sqrt(sqdist)) i.e. your gravity vector is in the direction of -p, so the sheep all go towards p = xero. Which looks in your video to be the lower-left corner of the screen. Try working out the mean position of the sheep (add up all the positions and divide by #sheep) and subtract that from p: Code: p = Vec2d(body.position) - mean_position with the rest the same. Obviously you'll need to do this every frame so the sheep move towards the centre of the flock. However, I suspect you'll get instability - if you're using a physics engine this kind of thing really ought to be implemented using constraints. If you find everything bounces all over try turning down your very high gravitational constant to stabilise things. I'd also go and read about Boids which attempts to produce exactly the kind of behaviour you want: http://en.wikipedia.org/wiki/Boids (http://en.wikipedia.org/wiki/Boids) In particular the bit about alignment, so your sheep head in the same direction as well as cluster together. HTH, Will Title: Re: Help with Chipmunk/Pymunk Planetary Physics Post by: NinthPower on November 06, 2013, 05:52:27 PM Thanks a ton Will, I'll check out your recommendations.
-9P |