• Advertisement

Circle-Circle impulse resolution not working properly

Recommended Posts

I am doing a little physics project with circle circle collisions for now, and have tried to do impulse resolution for collisions with 2 circles, using the following code. 

relativeVelocity = (other.doVerletVelocity()).subtract(self.doVerletVelocity())
normDirecVel = relativeVelocity.dotProduct(collisionNormal)

restitution = -1 - min(self.restitution, other.restitution)

numerator = normDirecVel * restitution

impulseScalar = numerator / float(1 / self.mass) + float(1 / other.mass)

selfVel = self.doVerletVelocity()
otherVel = other.doVerletVelocity()

impulse = collisionNormal.scalarMult(impulseScalar)

selfDV = impulse.scalarMult(1 / self.mass)
otherDV = impulse.scalarMult(1 / other.mass)

newSelfVel = selfVel.subtract(selfDV)
newOtherVel = otherVel.add(otherDV)

self.oldPos = (self.center).subtract(newSelfVel.scalarMult(dt))
other.oldPos = (other.center).subtract(newOtherVel.scalarMult(dt))

The problem seems to be that whatever value I give to self.mass and other.mass, the output stays exactly the same, the values that I used are:

center = Vector(0, 0)
radius = 1
oldPos = Vector(0, 0)
accel = Vector(0, 0)
mass = 100
restitution = 0.001

center2 = Vector(0, 3.20)
radius2 = 1
oldPos2 = Vector(0, 3.201)
accel2 = Vector(0, -1)
mass2 = 1
restitution2 = 1

the output was:

0.0      0.0      0.0      2.165000000000114
0.0      0.0      0.0      2.1360000000001174
0.0      0.0      0.0      2.1066000000001206
0.0      0.0      0.0      2.076800000000124
0.0      0.0      0.0      2.046600000000127
0.0      0.0      0.0      2.0160000000001306
0.0      0.0      0.0      1.985000000000134
CIRCLE INTERSECTION
0.0      -1.985000000000134      0.0      3.938600000000271
0.0      -3.970000000000268      0.0      5.891800000000408
0.0      -5.9550000000004015      0.0      7.844600000000544
0.0      -7.940000000000535      0.0      9.797000000000681

I changed the values for the masses to make them higher, bu the output still remained the same, if you could get to the bottom of this, it would be much appreciated.

Share this post


Link to post
Share on other sites
Advertisement

I noticed this:

impulseScalar = numerator / float(1 / self.mass) + float(1 / other.mass)

You are missing parentheses:

impulseScalar = numerator / ( float(1 / self.mass) + float(1 / other.mass) )

 

Share this post


Link to post
Share on other sites
23 hours ago, Dirk Gregorius said:

I noticed this:

impulseScalar = numerator / float(1 / self.mass) + float(1 / other.mass)

You are missing parentheses:

impulseScalar = numerator / ( float(1 / self.mass) + float(1 / other.mass) )

 

I tried that, yet it has no effect on the values. When displaying the circles on the screen, using pygame module, it showed that after colliding, the circles moved in opposite directions, but with velocities faster than what they collided with.

Edited by Bob Dylan
I needed to add more information.

Share this post


Link to post
Share on other sites
1 minute ago, Dirk Gregorius said:

Doesn't that appear strange to you? Obviously it should have an effect since :

n/a + b != n / (a + b) for any b != 0

It definitely is strange, yet what I described is the case. I think the error might be in the way that 1/mass is calculated because changing the mass when defining the particle object has not bearing on the resultant impulse.

Share this post


Link to post
Share on other sites

What language are you programming in? It might be that it is the type promotion. Try 

float(1.0f / self.mass)

 

Either way, this should be easy to find. Simply debug and step through the code.

 

Share this post


Link to post
Share on other sites
17 minutes ago, Dirk Gregorius said:

What language are you programming in? It might be that it is the type promotion. Try 

float(1.0f / self.mass)

 

Either way, this should be easy to find. Simply debug and step through the code.

 

Are you supposed to deal with penetration in the collision detection phase, or the impulse resolution phase? I ask this because that might be what is throwing this off, because as soon as they collide, the circles make a large jump, which might be affecting their velocities as i am just approximating velocity as current position - old position divided by the timestep, since my velocity verlet implementation does not deal with veloicty explicitly.

Edited by Bob Dylan

Share this post


Link to post
Share on other sites
Just now, Dirk Gregorius said:

You detect penetration in the collision phase. You can deal with it in the impulse phase or in a separate penetration recovery phase.

 

Thanks, I think that is what is causing the value of the velocities to be large after impulse resolution.

Share this post


Link to post
Share on other sites

That means, I would first not worry about penetration recovery and don't support restitution. In particular getting penetration recovery and restitution work correct together can be tricky.

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites
3 minutes ago, Dirk Gregorius said:

That means, I would first not worry about penetration recovery and don't support restitution.

But there was a rather helpful and in-depth tutorial that showed how to deal with it. Through I will follow your suggestion if my approach to change the penetration handling from the collision detection to impulse resolution section of the code and by the way - thanks for all the help and time that you have taken to help me. I'll let you know how it goes (if you care).

Edited by Bob Dylan

Share this post


Link to post
Share on other sites

One thing I learned over the past years is that with software development in general and physics development in particular is that you want to come to a working solution as fast and easy as possible. From there you want to add one feature at a time always iterating on a working solution.

You are currently seeing yourself what a pain it can be to search for a bug without having an idea where to look. If you use a smart incremental approach this helps to keep problems manageable. That is why I suggest getting the simplest problem working first and then add one feature at a time (e.g. penetration recovery and restitution).

I don't know which tutorial you are referring to so I cannot comment on this. With game physics you have the problem that there are a couple of good references by industry experts and then a bunch of students essentially rewrite these tutorial for studying purposes and quite often introduce errors reflecting their lack of understanding. So when looking at in-depth tutorial make sure they are from people who know what they are talking about.

 

Share this post


Link to post
Share on other sites
1 hour ago, Dirk Gregorius said:

One ... talking about.

 

I will take your advice on board when continuing with this simulation. However, if you want to inspect the tutorial I used, it is the following link.

https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331.

Share this post


Link to post
Share on other sites

Hi there, Dirk's advice is trying to describe a method to debug your code. The idea is if you get a working solution, then whenever you make small changes it is easy to know what broke. From here finding problems becomes much easier.

The tutorial you posted is best used by referencing the source code in the final article. The source code is here: https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-oriented-rigid-bodies--gamedev-8032

Unfortunately I don't have time to manually step through your code to look for problems. The best thing you can try doing is to take Dirk's advice and start with a small working solution. From there you can modify the working code to look more like your own code, and replace piece by piece until something breaks. This can give you clues on where problems lay.

Share this post


Link to post
Share on other sites
7 hours ago, Randy Gaul said:

Hi there, Dirk's advice is trying to describe a method to debug your code. The idea is if you get a working solution, then whenever you make small changes it is easy to know what broke. From here finding problems becomes much easier.

The tutorial you posted is best used by referencing the source code in the final article. The source code is here: https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-oriented-rigid-bodies--gamedev-8032

Unfortunately I don't have time to manually step through your code to look for problems. The best thing you can try doing is to take Dirk's advice and start with a small working solution. From there you can modify the working code to look more like your own code, and replace piece by piece until something breaks. This can give you clues on where problems lay.

Thanks for your advice, I realise that you do not have much time, but could I trouble you to explain to me when and were you deal with penetration, as I have since got the collision response working with restitution. If you would't mind, could you also explain to me how you would calculate the penetration vector for a circle-circle collision?

As at the moment, I am using this code:

PD = -(self.rad + other.rad) + math.sqrt(centDist)
angle = diff.xAxisAngle()
PDx = PD * math.cos(math.degrees(angle))
PDy = PD * math.sin(math.degrees(angle))
penetrationVec = Vector(PDx, PDy)
dP = penetrationVec.scalarMult(0.5)

self.center = (self.center).add(dP)
other.center = (other.center).subtract(dP)

While it does provide accurate enough collisions, it does however make the circles jump when I represent them on the screen. Thanks again.

Edited by Bob Dylan

Share this post


Link to post
Share on other sites

I'd like to help but the question you're asking is very open ended. There isn't really a specific piece I can help you with here. I'd love to help, but it sounds like you're not sure where the problem is yet.

Share this post


Link to post
Share on other sites

I would recommend adding a visual "step" by "step" debug system, so you clearly see when something goes wrong.

You can just use a bool to force to simulate one step or even step a single body pair. Its really hard to solve physics problems when you just look at the final output.

Render your bounding boxes + orientation and render your contact points inluding the surface normal and penetration distance. Start simple like dirk already recommended and build up from there.

Also making a realtime dragging tool to move/rotate two bodies around at realtime helps a lot!

I made such stuff in the past for javascript if you are interested: http://root.xenorate.com/final/jsstuff/

Edited by Finalspace

Share this post


Link to post
Share on other sites
5 hours ago, Randy Gaul said:

I'd like to help but the question you're asking is very open ended. There isn't really a specific piece I can help you with here. I'd love to help, but it sounds like you're not sure where the problem is yet.

Here is the problem, as explicitly as I can describe it: sometimes, when my circles are moving with velocities not perpendicular to the X or Y axes, when they are supposed to collide, they just get stuck in each other.

This is not what it would be like in real life, and I know that I am supposed to somehow find the penetration vector of the circles and them move them apart.

Now what I want you to do is to tell me how to get the penetration vector and then when to move the circles apart from each other.

Share this post


Link to post
Share on other sites
29 minutes ago, Bob Dylan said:

Now what I want you to do is to tell me how to get the penetration vector and then when to move the circles apart from each other.

That's not something answerable on a forum. You're basically asking how to create an entire physics sim. That said, the answers are in the link you posted earlier. If you can't find them in the link, I'm not really sure anyone can help you here.

29 minutes ago, Bob Dylan said:

Here is the problem, as explicitly as I can describe it: sometimes, when my circles are moving with velocities not perpendicular to the X or Y axes, when they are supposed to collide, they just get stuck in each other.

I appreciate the explicit description. I can't help you because what you are describing is not explicit, even though the description of it is clear. Your problem is vague, even though you described it nicely.

There are many reasons why circles may get stuck inside each other, and the problems could be anywhere. There is no simple answer of "how to make them not stick together".

 
Edited by Randy Gaul

Share this post


Link to post
Share on other sites

One thing you can do is to setup similar situations both in Randy's and your simulators and step through them side by side. Or write down an analytical solution and confirm your results in debugger. The problem is that you need to debug your code and nobody else is going to do this for you.

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Sean Meredith
      Hi all, I am starting to develop a tactics game and ran into a problem I had not thought of. I began by drawing a screen with a hex grid, and this is no big deal. I got that working fine. But, I realized it didn't look quite right. This is because in most strategy games, you're not looking straight down. There is a bit of a tilt. Attached is an example of what I mean. The hexagons on bottom are larger than the hexagons on top, and I'm unsure of how to go about adding this effect. Especially when you consider that some maps may be of different sizes. 
      I'm not sure if this is the right place to post something like this, but it seems as though some sort of linear transformation would be applied? No? I don't even know where to begin in a problem like this.
      Thanks.

    • By nick1
      Hello,

      I have limited programming experience in C++, but have always had a passion for games.  Where do I start?  I have a rough idea of the type of game I want to develop and am ready to commit the time necessary to learn new languages.  Are mobile games too difficult to begin with? Should I begin learning the basics of keyboard controls before attempting touch screens?  I would appreciate any input or advice!
      Thanks!
      Nick1
    • By khawk
      Dene Carter, Managing Directory @ Fluttermind LLC (@fluttermind)
      From Indie to Fable and Back. 30 Years of Wisdom.
      Started writing games in 1984 when he was 14 years old. What has he done in 33 years?
      Druid - Commodore 64 Original Dungeon Keeper core team Fable franchise and more Indie through AAA.
      "I am an idiot" - first learned in 1984, and every year after.
      Rockman - made $7500 for 2 weeks of work. Figured he could make 26 games a year, or $438k in today's money.
      Takeaway: Really stupid at 14.
      Even in 1980's, developer only got 12-14% royalties.
      (Side note: Dene is fun to listen to. Recommend this on the Vault when it goes online.)

      You are not your players.
      Made a black and white game on a Spectrum, which was color. Did it because he was poor. Problem is his audience were not poor, and had color TV's. Reviews were not nice. Players see things completely different to you. Do not forget that your players have not seen the game as much as you. Avoid difficulty/complexity-creep. The real world has distractions and beer - they don't care about your game as much as you do. Test your mobile game on the toilet - that's what your real players do. Fundamentally, players live inside their own brains, not yours. Those you ignore will ignore you in return. Design for your players' minds, not for you. Generalizing is Really useful
      "An expert who is too narrow has difficulty colaborationg" - Valve Employee Manual Did a lot of things over the course of his career. Everyone did everything in the 1980's and 1990's. Most developers generalized. Developing a broad skill-set aids communication. Large teams require collaboration and clear communication. Knowledge breeds respect (never say 'JUST'). 'Just' suggests a task is easy. It might not be. Ignorance is an energy. Don't forget you are human. You are designed to adapt and can do anything if you put your mind to it. Be a human. Learn a skill outside your area. Programmer learn how to 3D model. Artist learn how to code. Learn music, theater. Think of yourself as an artist. Rapid Team Growth is Dangerous
      "If your team can eat more than two pizzas, it's too large." Werner Vogels, Amazon VP/CTO Early Fable - 3 developers. Communication very easy. Later Fable, team grew bigger. At 12 people rumor mill started to develop. Can't have everyone in every meeting at same time. Pockets and cliques develop. Fred Brooks. Communication paths don't grow linearly. Team communication grows exponentially. [n * (n-1)] / 2 8 people on team, 28 connections. Ringelmann Effect - "Larger groups lead to less motivation & coordination & productivity." Decreased motivation - larger group, start to feel like a cog in the machine. Decreased coordination - communication pathways explode. Suggestion: Increase identifiability. Make sure everyone knows everyone's contribution. Most of all: think before growing your team. Blandness Has Its Uses
      Pursuit of excellence can be wasteful. Sounds counterintuitive. Blandness helps disguise repetition. Think reusing assets. Players notice the patterns. When asking for something to be made, communicate the context of assets - how will they be seen or heard? Often find they need to be bland. Prototypes Can Be Misleading
      Experiential games are difficult to prototype. More useful for mechanical games. Fable only came together at the very end - threw away at least one entire combat system. Looking back, it wasn't polished not necessarily bad. Bland prototypes are better than ugly ones for experiential. Keep prototype completely separate. Define prototypes success criteria. Art Style is More Important Than You Think
      Curate rather than create Style can hide the fact you can't draw. Restrict complexity. Style is marketing. Unique style tells players there may be something unique about your game. Streamline Your Process
      What is your iteration cost? Recognize your cost to try something and learn from it. Making your life easier is real work. Resist self-sabotage. (context of making tools) Closing Thoughts
      Don't let technology dictate your game's core promise. Static screenshots have to look good, too. No 1 pixel lines. Don't worry about the things people don't ever get to see. Don't panic if your game sucks - fix it. Editor thought: Really enjoyed this talk. Dene is a fun speaker, and his advice was raw, real world advice for anyone aspiring to make it in game development.
    • By Bokchee 88
      I am animator by hard, and i am doing game animation for at least 8 years so far. During the last 2 years, i came with a idea for game and maybe some day, i want to start indie game company. As i am thinking to start game company, i am also thinking what kind of value i can give to the company. For example, am experience in animation,sales(I was selling web development services, before i jumped to gaming), bit of rigging- just not for production, i am learning on the side as well. The rest of the gaming production, like modeling, concept art, texturing, i am total noob or to say better, i am no near interest to do modeling for example, don't have such a patience to do it. But before characters and things are made for animating, what the hell i am would do?
      Also, what is the ideal size of the founding team of a game company? Positions to be filled mostly are, Concept artist, Modeler/Texture artist, programmer, animator-rigger. And later would need more people to join, like more animators, programmers, sound, fx,etc.
       
      And lastly, do i need to have something,like a prototype, to show people and get them interest, or should i ask someone i know, for skill that i lack, for example, Modeling would be great, texturing and rigging, and to start all together from scratch?  
    • By Arthev
      Hey folks, new here
      I've decided to take the advice in this article.
      So any comments and criticisms on my pong clone would be appreciated!
      Written using Python 3 and Pygame.
      Github repo: available here.
      Thanks!
      import pygame import random import colour_constants import time import json import math from pathlib import Path SCREEN_SIZE = (640, 480) BALL_DIAMETER = 32 MAX_FPS = 60 PADDLE_SIZE = (16, 64) PADDLE_VEL = 10 HORIZONTAL_BAR = (SCREEN_SIZE[0], PADDLE_SIZE[0]) RECENT_HIT_RESET = 20 BLACK = colour_constants.DISPLAYBLACK PUREBLACK = colour_constants.PUREBLACK #Easy access for colorkeying SETTINGS_PATH = "config_pong.cfg" GAME_INTENSIFYING_CONSTANT = 1.08 COLOUR = colour_constants.APPLE2 player_one_up = pygame.K_q player_one_down = pygame.K_a player_two_up = pygame.K_o player_two_down = pygame.K_l points_per_game = 5 pygame.mixer.pre_init(44100, -16, 2, 2048) pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32) font = pygame.font.SysFont("Mono", 32) try: bounce_sound = pygame.mixer.Sound('bounce.wav') score_sound = pygame.mixer.Sound('score.wav') paddle_sound = pygame.mixer.Sound('paddle_bounce.wav') menu_sound = pygame.mixer.Sound('menu.wav') except: raise UserWarning("Couldn't load sound files.") class Play_Background: def __init__(self): self.surface = pygame.Surface(SCREEN_SIZE) self.surface.fill(BLACK) self.surface = self.surface.convert() pygame.draw.rect(self.surface, COLOUR, (0, 0, HORIZONTAL_BAR[0], HORIZONTAL_BAR[1])) pygame.draw.rect(self.surface, COLOUR, (0, SCREEN_SIZE[1] - HORIZONTAL_BAR[1], HORIZONTAL_BAR[0], HORIZONTAL_BAR[1])) pygame.draw.line(self.surface, COLOUR, (SCREEN_SIZE[0]//2, 0), (SCREEN_SIZE[0]//2, SCREEN_SIZE[1])) def draw(self) -> None: screen.blit( self.surface, (0, 0) ) class vector2: def __init__(self, x, y): self.x = x self.y = y def get_magnitude(self): return math.sqrt(self.x**2 + self.y**2) def normalize(self): magnitude = self.get_magnitude() self.x /= magnitude self.y /= magnitude def __add__(self, rhs): return vector2(self.x + rhs.x, self.y + rhs.y) def __mul__(self, scalar): return vector2(self.x * scalar, self.y * scalar) class Paddle: def __init__(self, side, player, up_button=None, down_button=None): if side == "left": self.pos = vector2(SCREEN_SIZE[0]//40, SCREEN_SIZE[1]//2 - PADDLE_SIZE[1]//2) elif side == "right": self.pos = vector2(SCREEN_SIZE[0] - PADDLE_SIZE[0] - SCREEN_SIZE[0]//40, SCREEN_SIZE[1]//2 - PADDLE_SIZE[1]//2) else: raise ValueError("Illegal 'side' argument sent to Paddle.__init__:", side) if player and (up_button == None or up_button == None): raise ValueError("Illegal combination of player and buttons sent to Paddle.__init__. None buttons make no sense for player == True") self.side = side self.player = player self.up_button = up_button self.down_button = down_button self.vel = vector2(0, 0) #No initial movement. self.surface = pygame.Surface( (PADDLE_SIZE[0], PADDLE_SIZE[1]) ) self.surface.fill(COLOUR) self.surface = self.surface.convert() self.score = 0 if not player: self.wait_to_calculate = 0 if not player: self.last_ball_dir = "left" if not player: self.expected_pos = 0 def calculate_expected_pos(self, ball): posVec = vector2(ball.pos.x + BALL_DIAMETER//2, ball.pos.y + BALL_DIAMETER//2) velVec = vector2(ball.vel.x, ball.vel.y) while True: timer = 1/(MAX_FPS) posVec.x += velVec.x * timer posVec.y += velVec.y * timer #Pseudo_horizontal_bar_check_and_adjustment if posVec.y - BALL_DIAMETER//2 < HORIZONTAL_BAR[1]: velVec.y *= -1 posVec.y = HORIZONTAL_BAR[1] + BALL_DIAMETER//2 elif posVec.y + BALL_DIAMETER//2 > SCREEN_SIZE[1] - HORIZONTAL_BAR[1]: velVec.y *= -1 posVec.y = SCREEN_SIZE[1] - BALL_DIAMETER//2 - HORIZONTAL_BAR[1] #Now for the bounce from the left side, if any if posVec.x - BALL_DIAMETER//2 < 0: posVec.x = BALL_DIAMETER//2 velVec.x *= -1 #And finally, handling for when found the expected pos if posVec.x + BALL_DIAMETER//2 > SCREEN_SIZE[0] - PADDLE_SIZE[0]: return posVec def calculate_expected_drift(self, case): yVel = self.vel.y yPos = self.pos.y while yVel > 5: yVel -= yVel / (MAX_FPS * 1.3) #movesim yPos += yVel * 1/MAX_FPS if yPos < HORIZONTAL_BAR[1]: yVel *= -0.75 yPos = HORIZONTAL_BAR[1] elif yPos + PADDLE_SIZE[1] > SCREEN_SIZE[1] - HORIZONTAL_BAR[1]: yVel *= -0.75 yPos = SCREEN_SIZE[1] - PADDLE_SIZE[1] - HORIZONTAL_BAR[1] if yPos + PADDLE_SIZE[1]//3 - self.expected_pos.y > PADDLE_SIZE[1]//4: return "under" elif (yPos + PADDLE_SIZE[1] - PADDLE_SIZE[1]//3) - self.expected_pos.y < -PADDLE_SIZE[1]//4: return "over" else: return "drift" def move(self, time_passed, ball) -> None: #Accelerate if self.player: pressed_keys = pygame.key.get_pressed() if pressed_keys[self.up_button]: self.vel.y -= PADDLE_VEL elif pressed_keys[self.down_button]: self.vel.y += PADDLE_VEL else: self.vel.y -= self.vel.y / (MAX_FPS * 1.3) #Found experimentally else: if self.expected_pos == 0: self.expected_pos = self.calculate_expected_pos(ball) if ball.vel.x > 0: self.last_ball_dir = "right" if self.wait_to_calculate > 0: self.wait_to_calculate -= 1 else: self.expected_pos = self.calculate_expected_pos(ball) self.wait_to_calculate = RECENT_HIT_RESET//2 elif ball.vel.x < 0: if self.last_ball_dir == "right": self.last_ball_dir = "left" self.expected_pos = self.calculate_expected_pos(ball) self.wait_to_calculate = 0 probable_drift = self.calculate_expected_drift("larger") #expected_pos y is larger than self y if probable_drift == "under": self.vel.y -= PADDLE_VEL elif probable_drift == "over": self.vel.y += PADDLE_VEL else: if abs(self.pos.y + PADDLE_SIZE[1]//2 - self.expected_pos.y) > PADDLE_SIZE[1]: if self.pos.y + PADDLE_SIZE[1]//2 > self.expected_pos.y: self.vel.y -= PADDLE_VEL else: self.vel.y += PADDLE_VEL self.vel.y -= self.vel.y / (MAX_FPS * 1.3) #Move self.pos.y += self.vel.y * time_passed if self.pos.y < HORIZONTAL_BAR[1] or self.pos.y + PADDLE_SIZE[1] > SCREEN_SIZE[1] - HORIZONTAL_BAR[1]: paddle_sound.play() self.vel.y *= -0.75 if self.pos.y < HORIZONTAL_BAR[1]: self.pos.y = HORIZONTAL_BAR[1] else: self.pos.y = SCREEN_SIZE[1] - PADDLE_SIZE[1] - HORIZONTAL_BAR[1] def draw(self) -> None: screen.blit(self.surface, (self.pos.x, self.pos.y)) class Ball: def reset(self) -> None: self.last_hit = None self.pos = vector2(SCREEN_SIZE[0]//2, SCREEN_SIZE[1]//2) self.vel = vector2(SCREEN_SIZE[0] * random.random(), SCREEN_SIZE[1] * random.random()) if random.random() < 0.5: self.vel.x *= -1 if random.random() < 0.5: self.vel.y *= -1 if abs(self.vel.x) < SCREEN_SIZE[0] * 0.1: self.reset() def __init__(self): self.pos = None self.vel = None self.surface = pygame.Surface( (BALL_DIAMETER, BALL_DIAMETER) ) self.surface.set_colorkey(PUREBLACK) pygame.draw.circle( self.surface, COLOUR, (BALL_DIAMETER//2, BALL_DIAMETER//2), BALL_DIAMETER//2) self.surface = self.surface.convert_alpha() self.last_hit = None self.reset() def horizontal_bar_check_and_adjustment(self) -> None: if self.pos.y < HORIZONTAL_BAR[1] or self.pos.y + BALL_DIAMETER > SCREEN_SIZE[1] - HORIZONTAL_BAR[1]: self.vel.y *= -1 bounce_sound.play() if self.pos.y < HORIZONTAL_BAR[1]: self.pos.y = HORIZONTAL_BAR[1] else: self.pos.y = SCREEN_SIZE[1] - BALL_DIAMETER - HORIZONTAL_BAR[1] def score_check(self) -> str: #Can also return None! if self.pos.x + BALL_DIAMETER < 0: return "right" elif self.pos.x > SCREEN_SIZE[0]: return "left" else: return None def simple_collision_check(self, paddles) -> Paddle: #Can also return None! left = paddles[0] right = paddles[1] if self.pos.x + BALL_DIAMETER < right.pos.x and self.pos.x > left.pos.x + PADDLE_SIZE[0]: return None #If we progress below here, a hit might happen... since the ball isn't *between* the paddles! if self.pos.x + BALL_DIAMETER > right.pos.x: #Ball might have hit the right paddle. if self.pos.y > right.pos.y + PADDLE_SIZE[1]: return None elif self.pos.y + BALL_DIAMETER < right.pos.y: return None else: return right else: #Ball might have hit the left paddle. if self.pos.y > left.pos.y + PADDLE_SIZE[1]: return None elif self.pos.y + BALL_DIAMETER < left.pos.y: return None else: return left def collision_handling(self, paddle) -> None: steps = PADDLE_SIZE[1]//2 + BALL_DIAMETER//2 extreme = 1.12 #arcsin(0.9) = 1.12 hit = self.pos.y + BALL_DIAMETER//2 - (paddle.pos.y + PADDLE_SIZE[1]//2) w = extreme/steps*hit newVec = 0 #This is a unit vector, hehe. if paddle.side == "left": newVec = vector2(-math.cos(w), -math.sin(w)) else: newVec = vector2(math.cos(w), -math.sin(w)) magnitude = self.vel.get_magnitude() * GAME_INTENSIFYING_CONSTANT * -1 self.vel.normalize() newVec = newVec + self.vel #The two vectors can be scalar multiplied by some percentage for different admixtures. newVec.normalize() newVec = newVec * magnitude self.vel = newVec def move(self, time_passed, paddles) -> None: self.pos.x += self.vel.x * time_passed self.pos.y += self.vel.y * time_passed self.horizontal_bar_check_and_adjustment() quick_collision = self.simple_collision_check(paddles) #quick_collision will contain a paddle - or None. if quick_collision: if quick_collision.side != self.last_hit: bounce_sound.play() self.last_hit = quick_collision.side self.collision_handling(quick_collision) def draw(self) -> None: screen.blit(self.surface, (self.pos.x, self.pos.y)) class Scoredrawer: def __init__(self): self.left = 0 self.right = 0 def draw(self, left, right): #Idea for improved performance: Check whether there's a change to a score and then only rendering if so. left_text = font.render(str(left), False, COLOUR).convert_alpha() right_text = font.render(str(right), False, COLOUR).convert_alpha() y_offset = HORIZONTAL_BAR[1] * 1.5 screen.blit(left_text, (SCREEN_SIZE[0]//2 - 32 - left_text.get_width(), y_offset)) screen.blit(right_text, (SCREEN_SIZE[0]//2 + 32, y_offset)) def display_winner(winner) -> None: winner_text = font.render( winner.capitalize() + " has won!", False, COLOUR).convert_alpha() screen.blit(winner_text, (SCREEN_SIZE[0]//2 - winner_text.get_width()//2, SCREEN_SIZE[1]//2 - winner_text.get_height()//2)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.constants.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: exit() elif event.key == pygame.K_RETURN: return def play(win_score: int, two_player_mode: bool) -> str: #returns 'left' or 'right' depending on which player won clock = pygame.time.Clock() background = Play_Background() scoredrawer = Scoredrawer() ball = Ball() paddle1 = Paddle(side="left", player=True, up_button=player_one_up, down_button=player_one_down) paddle2 = Paddle(side="right", player=two_player_mode, up_button=player_two_up, down_button=player_two_down) paddles = [paddle1, paddle2] #Left side must go in paddles[0], right side in paddles[1]. while True: for event in pygame.event.get(): if event.type == pygame.constants.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: exit() time_passed = clock.tick(MAX_FPS) / 1000.0 #time_passed is in seconds background.draw() ball.move(time_passed, paddles) for paddle in paddles: paddle.move(time_passed, ball) scorer = ball.score_check() #Either "left", "right" or None if scorer: score_sound.play() if scorer == "left": paddles[0].score += 1 if paddles[0].score >= win_score: display_winner(scorer) return else: paddles[1].score += 1 if paddles[1].score >= win_score: display_winner(scorer) return ball.reset() scoredrawer.draw(paddles[0].score, paddles[1].score) ball.draw() for paddle in paddles: paddle.draw() pygame.display.update() def display_intro() -> None: display_font = pygame.font.SysFont("mono", SCREEN_SIZE[0]//6) display_text = display_font.render("PongyPong", False, COLOUR, BLACK).convert() display_font2 = pygame.font.SysFont("mono", SCREEN_SIZE[0]//18) display_text2 = display_font2.render("by: Arthur", False, COLOUR, BLACK).convert() for i in range(256): screen.fill(BLACK) display_text.set_alpha(i) screen.blit(display_text, (SCREEN_SIZE[0]//2 - display_text.get_width()//2, SCREEN_SIZE[1]//2 - display_text.get_height())) pygame.display.update() time.sleep(1/100) for i in range(256): display_text2.set_alpha(i) screen.blit(display_text2, (SCREEN_SIZE[0]//2, SCREEN_SIZE[1]//2)) pygame.display.update() time.sleep(1/200) time.sleep(0.25) def settings_menu() -> None: def save_settings(dummy): current_settings = {'COLOUR':COLOUR, 'player_one_up':player_one_up, 'player_one_down':player_one_down, 'player_two_up':player_two_up, 'player_two_down':player_two_down, 'points_per_game':points_per_game} with open(SETTINGS_PATH, 'w') as settings_file: json.dump(current_settings, settings_file) def set_key(var): press_message = font.render( "Press the desired key...", False, COLOUR).convert_alpha() screen.blit(press_message, (SCREEN_SIZE[0]//2 - press_message.get_width()//2, SCREEN_SIZE[1] - font.get_linesize() * 1.5)) pygame.display.update() pygame.event.clear() while True: for event in pygame.event.get(): if event.type == pygame.constants.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: exit() if event.key != pygame.K_RETURN: menu_sound.play() globals()[var] = event.key return selection = 0 options = [{'Text': 'Points Per Game: ', 'func': lambda x: None, 'var': 'points_per_game', 'extradraw': ['arrows', 'value']}, {'Text': 'Colour', 'func': lambda x: None, 'var': 'COLOUR', 'extradraw':'arrows'}, {'Text': 'Player One Up: ', 'func': set_key, 'var': 'player_one_up', 'extradraw':'button'}, {'Text': 'Player One Down: ', 'func': set_key, 'var': 'player_one_down', 'extradraw':'button'}, {'Text': 'Player Two Up: ', 'func': set_key, 'var': 'player_two_up', 'extradraw':'button'}, {'Text': 'Player Two Down: ', 'func': set_key, 'var':'player_two_down', 'extradraw':'button'}, {'Text': 'Save Settings', 'func': save_settings, 'var': 'return', 'extradraw': None}] colour_options = [colour_constants.AMBER, colour_constants.LTAMBER, colour_constants.GREEN1, colour_constants.APPLE1, colour_constants.GREEN2, colour_constants.APPLE2, colour_constants.GREEN3] colour_selection = 0 for i, colour in enumerate(colour_options): if colour == COLOUR: colour_selection = i blinker = True while True: for event in pygame.event.get(): if event.type == pygame.constants.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: exit() elif event.key == pygame.K_RETURN: menu_sound.play() options[selection]['func'](options[selection]['var']) if options[selection]['var'] == 'return': return elif event.key in [pygame.K_DOWN, player_one_down, player_two_down]: menu_sound.play() if selection == len(options) - 1: selection = 0 else: selection += 1 elif event.key in [pygame.K_UP, player_one_up, player_two_up]: menu_sound.play() if selection == 0: selection = len(options) - 1 else: selection -= 1 elif event.key == pygame.K_RIGHT: var = options[selection]['var'] if var == 'COLOUR': menu_sound.play() if colour_selection == len(colour_options) -1: colour_selection = 0 else: colour_selection += 1 globals()[var] = colour_options[colour_selection] elif var == 'points_per_game': menu_sound.play() globals()[var] += 1 elif event.key == pygame.K_LEFT: var = options[selection]['var'] if var == 'COLOUR': menu_sound.play() if colour_selection == 0: colour_selection = len(colour_options) - 1 else: colour_selection -= 1 globals()[var] = colour_options[colour_selection] if var == 'points_per_game': menu_sound.play() globals()[var] = max(1, points_per_game - 1) screen.fill(BLACK) DIV = 32 for i in range(len(options)): if i == selection: blinker = not blinker if blinker: continue text = font.render(options[i]['Text'], False, COLOUR).convert_alpha() screen.blit(text, (SCREEN_SIZE[0]//2 - text.get_width()//2, SCREEN_SIZE[1]//DIV + font.get_linesize() * 1.5 * i)) extradraw = options[i]['extradraw'] if extradraw == 'arrows' or (type(extradraw) == list and 'arrows' in extradraw): h = text.get_height() * 0.75 // 1 arrowsurface = pygame.Surface( (h, h) ) arrowsurface.fill(BLACK) pygame.draw.polygon(arrowsurface, COLOUR, ( (0, h//2), (h//2 - h//8, h//4), (h//2 - h//8, 3*h//4) ) ) pygame.draw.polygon(arrowsurface, COLOUR, ( (h, h//2), (h//2 + h//8, h//4), (h//2 + h//8, 3*h//4) ) ) arrowsurface.convert() screen.blit(arrowsurface, (SCREEN_SIZE[0]//2 - text.get_width()//2 - arrowsurface.get_width() - 2, SCREEN_SIZE[1]//DIV + font.get_linesize() * 1.5 * i + h//8)) if extradraw == 'value' or (type(extradraw) == list and 'value' in extradraw): valuesurface = font.render( str(globals()[options[i]['var']]), False, COLOUR).convert_alpha() screen.blit(valuesurface, (SCREEN_SIZE[0]//2 + text.get_width()//2 + 2, SCREEN_SIZE[1]//DIV + font.get_linesize() * 1.5 * i)) if extradraw == 'button' or (type(extradraw) == list and 'button' in extradraw): buttonsurface = font.render( pygame.key.name(globals()[options[i]['var']]), False, COLOUR).convert_alpha() screen.blit(buttonsurface, (SCREEN_SIZE[0]//2 + text.get_width()//2 + 2, SCREEN_SIZE[1]//DIV + font.get_linesize() * 1.5 * i)) pygame.display.update() def main_menu() -> None: def one_player_mode(): play(points_per_game, False) def two_player_mode(): play(points_per_game, True) selection = 0 options = [{'Text': '1-Player', 'func': one_player_mode}, {'Text': '2-Player', 'func': two_player_mode}, {'Text': 'Settings', 'func': settings_menu}, {'Text': 'Exit', 'func': exit}] blinker = True while True: for event in pygame.event.get(): if event.type == pygame.constants.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: exit() elif event.key == pygame.K_RETURN: menu_sound.play() options[selection]['func']() elif event.key in [pygame.K_DOWN, player_one_down, player_two_down]: menu_sound.play() if selection == len(options) - 1: selection = 0 else: selection += 1 elif event.key in [pygame.K_UP, player_one_up, player_two_up]: menu_sound.play() if selection == 0: selection = len(options) - 1 else: selection -= 1 screen.fill(BLACK) pseudopaddle = pygame.Surface( (PADDLE_SIZE[0]//2, PADDLE_SIZE[1]//2) ) pseudopaddle.fill(COLOUR) pseudopaddle = pseudopaddle.convert() screen.blit(pseudopaddle, (SCREEN_SIZE[0]//2 - PADDLE_SIZE[1], SCREEN_SIZE[1]//10 - PADDLE_SIZE[0]//2)) screen.blit(pseudopaddle, (SCREEN_SIZE[0]//2 + PADDLE_SIZE[1], SCREEN_SIZE[1]//10 + PADDLE_SIZE[0]//2)) pseudoball = pygame.Surface( (BALL_DIAMETER//2, BALL_DIAMETER//2) ) pseudoball.fill(BLACK) pygame.draw.circle( pseudoball, COLOUR, (BALL_DIAMETER//4, BALL_DIAMETER//4), BALL_DIAMETER//4) pseudoball = pseudoball.convert() screen.blit(pseudoball, (SCREEN_SIZE[0]//2 + BALL_DIAMETER//4, SCREEN_SIZE[1]//10 - BALL_DIAMETER//4)) for i in range(len(options)): if i == selection: blinker = not blinker if blinker: continue text = font.render(options[i]['Text'], False, COLOUR).convert_alpha() screen.blit(text, (SCREEN_SIZE[0]//2 - text.get_width()//2, SCREEN_SIZE[1]//4 + font.get_linesize() * 2 * i)) pygame.display.update() def load_settings() -> None: settings_path = Path(SETTINGS_PATH) if settings_path.is_file(): with open(SETTINGS_PATH, 'r') as settings_file: settings = json.load(settings_file) for item in settings: globals()[item] = settings[item] else: default_settings = {'COLOUR':COLOUR, 'player_one_up':player_one_up, 'player_one_down':player_one_down, 'player_two_up':player_two_up, 'player_two_down':player_two_down, 'points_per_game':points_per_game} with open(SETTINGS_PATH, 'w') as settings_file: json.dump(default_settings, settings_file) if __name__ == '__main__': load_settings() display_intro() main_menu()  
  • Advertisement