• Simple physics sandbox (Python / pyGame / pyMunk)
    42 replies, posted
Here a little sandbox game made by me (I just learned how to use pyMunk and pyGame). You can add more than 500 balls, and regulate the gravity. For now you can't do anything special: you can just spawn balls and change the gravity, so they can fall down out of the screen. I have made a video showing the game at its state. Maybe I will post updates soon, so stay tuned :) [media]http://www.youtube.com/watch?v=bo4viFGFdAk[/media] Here is the code of the entire program. To run it you will need to install: -[url=http://www.python.org/download/releases/2.5.4/]Python version 2.5.4[/url] -[url=http://www.pygame.org/ftp/pygame-1.8.1release.win32-py2.5.msi]pyGame for Python 2.5[/url] -[url=http://pymunk.googlecode.com/files/pymunk-0.8.2.win32.exe]pyMunk installer[/url] Here is the complete code if you want to try and modify it. [lua]import sys import random import math import pygame from pygame.locals import * from pygame.color import * import pymunk as pm def add_ball(space, position): radius = random.randint(5, 10) mass = radius inertia = pm.moment_for_circle(mass, 0, radius, (0, 0)) body = pm.Body(mass, inertia) x, y = position x = random.randint(x-2, x+2) y = random.randint(y-2, y+2) body.position = x, y shape = pm.Circle(body, radius, (0, 0)) space.add(body, shape) shape.color = random.randrange(50, 200), 0, 0 return shape def draw_ball(screen, ball): p = int(ball.body.position.x), 600-int(ball.body.position.y) # rad = ball.radius # # rect = pygame.rect.Rect(p[0]-rad, p[1]-rad, rad*2, rad*2) # # pygame.draw.rect(screen, ball.color, rect) pygame.draw.circle(screen, ball.color, p, int(ball.radius), 0) def add_l(space): body = pm.Body(pm.inf, pm.inf) body.position = (0, 0) l1 = pm.Segment(body, (400, 50), (590, 150.0), 3.0) l1b = pm.Segment(body, (590, 150), (590, 590.0), 3.0) l2 = pm.Segment(body, (10, 150), (200, 50), 3.0) l2b = pm.Segment(body, (10, 150), (10, 590.0), 3.0) lc = pm.Segment(body, (10, 590), (590, 590), 3.0) lt1 = pm.Segment(body, (250, 0), (300, 65), 3.0) lt2 = pm.Segment(body, (300, 65), (350, 0), 3.0) space.add(l1, l2, l1b, l2b, lc, lt1, lt2) return l1, l2, l1b, l2b, lc, lt1, lt2 def draw_lines(screen, lines): for line in lines: body = line.body pv1 = body.position + line.a.rotated(math.degrees(body.angle)) pv2 = body.position + line.b.rotated(math.degrees(body.angle)) p1 = to_pygame(pv1) p2 = to_pygame(pv2) pygame.draw.lines(screen, THECOLORS["lightgray"], False, [p1, p2]) def to_pygame(p): return int(p.x), int(-p.y+600) def to_pygame_tup(p): return int(p[0]), int(-p[1]+600) def main(): pygame.init() screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption("PyMunk") clock = pygame.time.Clock() running = True pm.init_pymunk() space = pm.Space() gravity = 0.0 space.gravity = (0.0, gravity) balls = [] lines = add_l(space) font = pygame.font.Font(None, 50) while running: for event in pygame.event.get(): if event.type == QUIT: running = False elif event.type == KEYDOWN and event.key == K_ESCAPE: running = False if event.type == MOUSEBUTTONDOWN: if event.button == 4: gravity += 50.0 space.gravity = (0.0, gravity) elif event.button == 5: gravity -= 50.0 space.gravity = (0.0, gravity) if pygame.mouse.get_pressed()[0]: x, y = pygame.mouse.get_pos() ball_shape = add_ball(space, to_pygame_tup((x, y))) balls.append(ball_shape) if pygame.mouse.get_pressed()[2]: x, y = pygame.mouse.get_pos() for ball in balls: ball.body.apply_impulse(ball.body.position, to_pygame_tup((x, y))) for ball in balls: #ball.body.apply_impulse((50, 50), (50,50)) if ball.body.position.y < 0-ball.radius: balls.remove(ball) elif ball.body.position.y > 600+ball.radius: balls.remove(ball) gravnum = font.render("""Gravity = %d"""%gravity, True, (159, 182, 205), (255, 255, 255)) gravrect = gravnum.get_rect() gravrect.centerx = screen.get_rect().centerx gravrect.centery = screen.get_rect().centery-20 ballnum = font.render("""Balls = %d"""%len(balls), True, (159, 182, 205), (255, 255, 255)) ballrect = ballnum.get_rect() ballrect.centerx = screen.get_rect().centerx ballrect.centery = screen.get_rect().centery+20 #PAINTING screen.fill(THECOLORS["white"]) screen.blit(gravnum, gravrect) screen.blit(ballnum, ballrect) for ball in balls: draw_ball(screen, ball) draw_lines(screen, lines) space.step(1/50.0) pygame.display.flip() clock.tick(50) if __name__ == '__main__': sys.exit(main())[/lua]
Awesome. I wish I knew python [editline]02:25PM[/editline] Awesome. I wish I knew python
Can you clarify something for me? [code] ball.radius [/code] Is a "ball" a class or what? I am confused as where ball has come from.
Do you mean [code]int(ball.radius)[/code] in the function draw_ball? ball is one of the arguments of the call. ball is a shape. It is returned by the add_ball function.
Where is the code for the physics? I don't really know Python, but I can't seem to find it. Is it included in PyGame?
pyMunk is probably a python port of the Chipmunk 2D physics library.
[QUOTE=nullsquared;15729563]pyMunk is probably a python port of the Chipmunk 2D physics library.[/QUOTE] Oh, I thought PyMunk was the name of his program. :downs: Thanks.
Cool. I made something just like this in XNA, but the circle collision was buggy as hell. The spheres would spin around each other. D:
[QUOTE=nullsquared;15729563]pyMunk is probably a python port of the Chipmunk 2D physics library.[/QUOTE] Yes exactly. With python you can do almost everything you can do with c++ because there are a lot of binding libraries. The only problem is that python is just a bit slower (it is not a compiled language, it's interpreted)
Huh, no. It's not just "a little bit slower" than C++, it's [url=http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=gpp&lang2=python&box=1]hella fucking slower at most big computing tasks.[/url]
When people make Python bindings for a C++ library, they aren't porting the library to Python, they are just making it so that those C++ functions can be called from Python (just like any other scripted language) As a result, Python is not considerably slower than pure C++, since most of the computation is in C++ anyways.
I understand that. But the language itself is what I'm talking about. Maybe it wasn't what TerabyteS was though.
[QUOTE=gparent;15739026]Huh, no. It's not just "a little bit slower" than C++, it's [url=http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=gpp&lang2=python&box=1]hella fucking slower at most big computing tasks.[/url][/QUOTE] :downsbravo: The OP isn't making Crysis. Python is fine.
[QUOTE=nullsquared;15740262]The OP isn't making Crysis. Python is fine.[/QUOTE] I don't really care about the game he made. I'm talking about python in general. :downsbravo::downsbravo::downsbravo:
[QUOTE=gparent;15740290]I don't really care about the game he made. I'm talking about python in general. :downsbravo::downsbravo::downsbravo:[/QUOTE] Then go make another thread. The OP isn't making binary trees or a mandelbrot or anything else that your test uses. Your argument is null and void.
Well, the game uses a c++ dll, so the computation is done by c++
[QUOTE=TerabyteS;15740953]Well, the game uses a c++ dll, so the computation is done by c++[/QUOTE] That's what I'm saying, but gparent is being a mindless C++ zealot.
[QUOTE=nullsquared;15740994]That's what I'm saying, but gparent is being a mindless C++ zealot.[/QUOTE] I'm not you idiot, python is fucking fine for the game he's writing (especially if he can get better performance using bindings). It has nothing to do with python performance in general which is what I thought he was talking about earlier. [quote=gparent]Maybe it wasn't what TerabyteS was though.[/quote] There, just so you don't miss it. He wasn't talking about what I thought he was, now get over it.
[QUOTE=nullsquared;15740994]That's what I'm saying, but gparent is being a mindless C++ zealot.[/QUOTE] thisthisthis
[QUOTE=gparent;15741031]It has nothing to do with python performance in general[/QUOTE] python performance in general != mandelbrots, etc.
Can everyone shut up?
[QUOTE=nullsquared;15742061]python performance in general != mandelbrots, etc.[/QUOTE] :ughh: Just forget it, I really couldn't care less about this bickering shit. [QUOTE=x0r;15742029]thisthisthis[/QUOTE] Yay, more ignorance. Just because I used C++ as an example doesn't mean it doesn't apply to a ton of other languages that are also consistently way faster than python. But guess what? It doesn't matter anymore because the OP uses python *bindings*, which we've discussed a few posts back now. It's time to let go now.
[QUOTE=gparent;15743112]:ughh: Just forget it, I really couldn't care less about this childish shit.[/QUOTE] Then stop saying stupid shit like "Huh, no. It's not just "a little bit slower" than C++, it's hella fucking slower at most big computing tasks." Instead of commenting on the OP's post, you just look at peoples posts and pick a fight and try to prove them wrong with your little statistics.
[QUOTE=x0r;15743174]Then stop saying stupid shit like "Huh, no. It's not just "a little bit slower" than C++, it's hella fucking slower at most big computing tasks." Instead of commenting on the OP's post, you just look at peoples posts and pick a fight and try to prove them wrong with your little statistics.[/QUOTE] Kind of ironic coming from a guy who quoted one of null's post for no reason just because he called me a zealot. When it was completely wrong. Do as I say, not as I do.
Where are you planning on going with this (what yet-to-come features do you have in mind)? Will it stay a sandbox/simulation or do you want to turn it into a game? Err... I meant to say needs moar derailing
[QUOTE=gparent;15743248]Kind of ironic coming from a guy who quoted one of null's post for no reason just because he called me a zealot. When it was completely wrong. Do as I say, not as I do.[/QUOTE] no reason? :ughh:
[QUOTE=ZeekyHBomb;15743266]Where are you planning on going with this (what yet-to-come features do you have in mind)? Will it stay a sandbox/simulation or do you want to turn it into a game? Err... I meant to say needs moar derailing[/QUOTE] Well, for now it's just barebones, and I don't think i will make a game out of it.. I will add some features like adding boxes or polygons, maybe color change and joints... we'll see.
Looking pretty cool so far - what friction do the balls have? Kind of reminds me of fluid physics with the moderate gravity :D
[QUOTE=nullsquared;15749325]Looking pretty cool so far - what friction do the balls have? Kind of reminds me of fluid physics with the moderate gravity :D[/QUOTE] I don't know the friction, it's calculated by pymunk when I create the ball body.
[QUOTE=nullsquared;15749325]Looking pretty cool so far - what friction do the balls have? Kind of reminds me of fluid physics with the moderate gravity :D[/QUOTE] This made me think that you should try and make some form of fluid simulation. All you need to do is make it up of balls but render it as one big block of colour.
Sorry, you need to Log In to post a reply to this thread.