Note that there are some explanatory texts on larger screens.

plurals
  1. POPython & Pygame: Ball collision with interior of circle
    primarykey
    data
    text
    <p>I'm making a game in which balls bounce around the <em>inside</em> of a much larger circle. The larger circle doesn't move.</p> <p>Here's the code that I'm currently using for these collisions:</p> <pre><code>def collideCircle(circle, ball): """Check for collision between a ball and a circle""" dx = circle.x - ball.x dy = circle.y - ball.y distance = math.hypot(dx, dy) if distance &gt;= circle.size + ball.size: # We don't need to change anything about the circle, just the ball tangent = math.atan2(dy, dx) ball.angle = 2 * tangent - ball.angle ball.speed *= elasticity + 0.251 angle = 0.5 * math.pi + tangent ball.x -= math.sin(angle) ball.y += math.cos(angle) </code></pre> <p>It is based on the wonderful tutorial by Peter Collingridge <a href="http://www.petercollingridge.co.uk/pygame-physics-simulation/collisions" rel="noreferrer">over here</a>.</p> <p>The circle and ball objects are both classes, with (x,y), radius, angle and speed.</p> <p>I am having two problems with this method, however:</p> <ol> <li>The ball bounces from (what I suspect) is its "anchor point", which seems to be in the top right corner of the circle.</li> <li>When colliding with the bottom 5% of the circle, is fails to bounce high enough and therefore "sinks" out of the screen. I am guessing that this is because the bounce is not high enough to move the ball above its (incorrectly placed) "anchor point"?</li> </ol> <p>Having looked at possible solutions already on here, notably "Fast circle collision detection" [Link deleted due to spam link limit], which, although in Java is using the same method, these all deal with external collisions, whereas I am looking at bouncing a ball around the interior of a circle.</p> <p>Here is also the class definitions of the Ball() and the Circle():</p> <pre><code>class Ball(): def __init__(self, (x,y), size): """Setting up the new instance""" self.x = x self.y = y self.size = size self.colour = (0,128,255) self.thickness = 0 self.speed = 0.01 self.angle = math.pi/2 def display(self): """Draw the ball""" pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness) def move(self): """Move the ball according to angle and speed""" self.x += math.sin(self.angle) * self.speed self.y -= math.cos(self.angle) * self.speed (self.angle, self.speed) = addVectors((self.angle, self.speed), gravity) self.speed *= drag class Circle(): def __init__(self, (x,y), size): """Set up the new instance of the Circle class""" self.x = x self.y = y self.size = size self.colour = (236, 236, 236) self.thickness = 0 self.angle = 0 # Needed for collision... self.speed = 0 # detection against balls def display(self): """Draw the circle""" pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness </code></pre> <p>Thanks in advance, Nathan</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload