kinetic balls

Official forum for the Chipmunk2D Physics Library.
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

kinetic balls

Post by pabloruiz55 »

Hi! Do you think that if i set up 5 circles with joints so they resemble a kinetic balls toy(?) it would work like in real life without tweaking much? Thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: kinetic balls

Post by slembcke »

It would work, though you'd probably want to use quite a few elastic iterations.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: kinetic balls

Post by viblo »

I made one a couple of days ago and it works really good, I was a bit surprised how nice it turned out :)
In my little example I made 10 calls to step each frame (running at 50fps), and stepsize 1/500.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: kinetic balls

Post by pabloruiz55 »

Hi, thanks!

I will try it soon then!
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: kinetic balls

Post by pabloruiz55 »

I set it up and works, but i am having problems confiruring it to look somewhat real, would you mind telling me what values you used to make it look good?
For example one problem i have is that, right now to make them rebound fast (what i want) i am setting the gravity to a really high value but it makes them behave erraticly sometimes.

Thank you very much and happy New Year!
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: kinetic balls

Post by Rawlyn »

(For those of you who have never heard of "kinetic balls", the alternative name for this setup is "Newton's cradle" http://en.wikipedia.org/wiki/Newtons_cradle.)

One of the first things I wrote with pymunk was a 3-ball Newton's cradle, and it seemed to work fine. Good luck :)
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: kinetic balls

Post by pabloruiz55 »

I am having this problem, sometimes the balls just dont rebound as they should, i alway release them at the same height and 1 out of 5 times they don't behave as they should, like they all start bouncing with each other instead of the ones in the ends.

Maybe i am setting it not perfect enough. I have 5, 25 radius balls each one just beside the other.i have a pin joint that goes from they center to a box up. these joints have a 50 px separation so they are perfectly perpendicular to the box when resting.

I have tried with really high iterations and elastic iterations and it doesn't make a difference.
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: kinetic balls

Post by Rawlyn »

Well, I don't know if this helps you C guys, but I whipped up a working version with pymunk (after searching the depths of my various hard drives for my original version - to no avail). The nice thing about python is that it pretty much reads like pseudo-code, so it may be of some help anyway.

Code: Select all

import pymunk
from pymunk.vec2d import Vec2d

import pygame
from pygame.locals import *

from math import pi


# ball class
class Ball(object):
	def __init__(self, space, radius, static_body, position, length):
		self.space = space
		self.radius = radius
		self.static_body = static_body
		self.position = Vec2d(position)
		self.length = length
		
		self.mass = pi*(self.radius**2)
		self.moment = pymunk.moment_for_circle(self.mass, 0, self.radius)
		self.body = pymunk.Body(self.mass, self.moment)
		self.body.position = self.position
		
		self.shape = pymunk.Circle(self.body, self.radius)
		self.shape.friction = 0.0
		self.shape.elasticity = 0.999
		
		self.jointpos = self.position-Vec2d(0,self.length)
		self.joint = pymunk.PinJoint(self.body, self.static_body, (0,0), self.jointpos)
		
		self.space.add(self.body)
		self.space.add(self.shape)
		self.space.add(self.joint)
		
	def draw(self, surface):
		x, y = self.body.position
		x, y = int(x), int(y)
		jx, jy = self.jointpos
		jx, jy = int(jx), int(jy)
		r = int(self.radius)
		pygame.draw.line(surface, (200,200,200), (jx,jy), (x,y), 3)
		pygame.draw.circle(surface, (255,255,255), (x,y), r, 3)


# init pygame
WIDTH = 800
HEIGHT = 600

pygame.init()

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('newtons cradle')

clock = pygame.time.Clock()


# init pygame
pymunk.init_pymunk()

space = pymunk.Space(25)
space._space.contents.elasticIterations = 15
space.gravity = (0,750)

fps = 60
steps = 15
stepsize = (1.0/fps)/steps


# create balls
static_body = pymunk.Body(pymunk.inf, pymunk.inf)

balls = []
for i in range(5):
	x = (WIDTH*0.5)+((i-2)*50)
	y = (HEIGHT*0.5)+150
	ball = Ball(space, 25, static_body, Vec2d(x,y), 300)
	balls.append(ball)

for i in range(3):
	balls[i].body.apply_impulse(Vec2d(-1000000,0))


# main loop
quit = False
while not(quit):
	# handle events
	for event in pygame.event.get():
		if event.type == QUIT:
			quit = True
			
	# update physics
	for step in range(steps):
		space.step(stepsize)
			
	# draw everything
	screen.fill((0,0,0))
	
	for ball in balls:
		ball.draw(screen)
	
	# update display
	clock.tick(fps)
	pygame.display.update()
Good luck with the C implementation! :)
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: kinetic balls

Post by viblo »

After having read about your troubles I made some more tests with my small script, and all the time the middle balls starts to bump around if I just wait long enough, even with lots of iterations and small steps.. Before I always looked at it for the first 10-20 rounds and then closed it and therefor I didn't see it. Now I don't even think its possible to do a stable cradle without cheating (=> some sort of damping to avoid that it blows up.. rotational friction, lower space.damping, a very soft spring to pull them to rest or something similar).

Rawlyn: I see you set the elasticIterations on your own with _space. Does it make sense to expose elasticIterations (and the normal iterations) as a property directly on the Space object instead? Or do you think its enough with how it is now when you can pass it in to the Space constructor only? (btw, if you don't know it already, you can pass in tuples instead of Vec2d's to all pymunk methods)
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: kinetic balls

Post by Rawlyn »

Hmm... I just left my example running for a while and I can see what you mean. Damn... lol :P As you say though, a bit of hacking here and there can work wonders. I'll have a look at it tomorrow when I'm a bit more sober. Ahem.

It seems it's been a while since I checked the pymunk documentation - I didn't realise I could specify elastic iterations in the constructor. I can't imagine a situation where you'd ever need to alter it at any time other than creation time, so I can't see much reason for exposing it as a property.

Thanks for the hint about passing tuples. I guess old habits die hard - my first language was Pascal, so my brain seems to automatically typecast things just to make sure!
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests