2
\$\begingroup\$

I'm learning how to program simple two-dimensional games in C++ using SFML, I've done a relatively simple pong game already I would like to ask how I could go about creating the physics for a curved edge such as one on an air hockey table Such as this:

Do I have to code a type of reflection of xVelocity/yVelocity for every single point or what? How do I even add a curved edge such as that in the first place?

\$\endgroup\$
  • \$\begingroup\$ Can you clarify what parts have curved edges? Do you mean the puck and the things the player holds on to? Or are you referring to the rounded corners of the table? \$\endgroup\$ – user1118321 Dec 16 '16 at 0:21
  • \$\begingroup\$ @user1118321 Yes! I apologize, I'm talking about the curved edges of the table. I could always just use the sf::CircleShape for the paddles I think. \$\endgroup\$ – Lucas Leodler Dec 16 '16 at 0:38
  • \$\begingroup\$ I removed the second question as it was not really related to the first one. You can post a second question :) \$\endgroup\$ – Vaillancourt Dec 16 '16 at 2:31
  • \$\begingroup\$ @AlexandreVaillancourt Understood, thank you :) as I get farther into the project and if I cannot find an answer from another source I will, I just didn't know if it would be better to append it onto the end of my original or create a whole new thread. \$\endgroup\$ – Lucas Leodler Dec 16 '16 at 5:37
  • \$\begingroup\$ Now you know :) given the nature of this site, asking more than one question per question post increases the chance of having them not all answered, so this is why we limit questions posts to one question. Now if the questions are all linked together you can ask them in the same post :) \$\endgroup\$ – Vaillancourt Dec 16 '16 at 11:04
0
\$\begingroup\$

I'd suggest looking into a physics library/engine like Box2D or bulletphysics.

If your primary interest is getting the circles to behave like pucks on ice, then just add the library to your project to have the collision handling - including the frictional responses (that deal with spin) - done for you.

If learning about handling the physics is your primary goal, then take a look at the library's documentation, tutorials, white papers, and/or source code. You'll probably want to focus on material related to continuous collision detection (CCD).

In Box2D for instance, there's:

  • Code for generating a collision description (code related to the b2Manifold);
  • Code for dealing with resolving positional overlap (like in SolvePositionConstraints);
  • Code for dealing with resolving velocity responses (see SolveVelocityConstraints); and
  • Code for calculating an upcoming time of impact (see b2TimeOfImpact).

Depending on how realistic you want your collisions to look, you may need to learn about all of these things and more.

Note that realism is typically at the expense of loss of performance. This shows up in terms of how many frames can be simulated and rendered per second. So a compromise has to be struck between these.

\$\endgroup\$
0
\$\begingroup\$

Since you're asking about physics, I assume you want to do things like reflect the puck off of the rounded corners, correct? You currently have code to reflect the puck off of the straight edges along the sides and top and bottom, so the easiest thing to do is to simply convert the rounded corners into a series of connected straight edges and re-use the straight-edge code you already have. I bet if you replaced each corner with a single diagonal edge, most users wouldn't even notice. But you can probably do better with 3-4 small line segments.

For reflections, you may recall from physics class that "the angle of reflection is equal to the angle of incidence." What this translates to is the angle the puck's vector of direction makes with the surfaces normal should be reflected across the normal to figure out where the puck is going to go to. See this question for a description of the math.

Note that for circles (or semi-circles), the surface normal is just the direction vector from the point on the edge of the circle where the puck hits to the center of the circle. So in this case, you could even use the actual value without doing too much work.

\$\endgroup\$
  • \$\begingroup\$ @user1118231 Thank you! I will try this :) \$\endgroup\$ – Lucas Leodler Dec 16 '16 at 1:02
  • \$\begingroup\$ This will probably not work very realistically because you are ignoring spin. You should consider using something like box2d. \$\endgroup\$ – wolfdawn Jan 18 '17 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.