I need help in physics or more exactly 'bouncing' for my game.

Discussion in 'Computer Programming, Emulation, and Game Modding' started by ilman, Aug 12, 2013.

  1. ilman
    OP

    ilman Gbatemp's Official Noise Eraser

    Member
    1,130
    243
    Jul 25, 2010
    Shibuya
    Hello to all. I'm developing a game in C++ and SFML. I need help with a part of it.
    I have 2 circles. One of them you control with the mouse and the other one should move on its own, but it doesn't hit the first circle properly. Let me explain.
    The second circle has a direction (in degrees, counting clockwise from North) and speed. It successfully bounces off the borders of the window in a realistic fashion. Now here comes the wrong part.
    Whenever the second circle hits the first one, it should bounce off in a realistic fashion, as well.
    After at least 10 different tries to calculate a formula for the new direction value, I gave up. The 2nd circle would either bounce off in a completely wrong direction, get stuck in the 1st one or, very strangely, start rotating round it.
    Now, I should mention that I have a very light understanding of trigonometry. I have never studied it in school (that'll be happening next year) and I tried to make out as much as I could from the Wikipedia article on trigonometric functions.

    I really hope someone could help me out with this, as it would solve pretty much all the problems with my game (except for speed calculation, but I have an idea about that).

    Thanks in advance.
     
  2. Arras

    Arras GBAtemp Guru

    Member
    5,840
    2,659
    Sep 14, 2010
    Netherlands
    My guess would be:
    Calculate the precise point where the two balls touch. If they overlap and there are two intersection points, take the center of those.
    Draw a line through the center of the mouse ball and the intersection point.
    Invert the speed vector of the moving ball and mirror it over said line. (alternatively, rotate this line by 90 degrees and use it as a wall for a regular bounce if your functions support that)
    How you'd do this in C++, I don't really know, but maybe that gives you an idea?
    I'm also not really sure whether this is correct, but I think it should work in most cases.
    That's only if the mouse moves somewhat slowly though. If you're waving it around like crazy I have no idea what would happen.
     
  3. ilman
    OP

    ilman Gbatemp's Official Noise Eraser

    Member
    1,130
    243
    Jul 25, 2010
    Shibuya
    The problem is that in C++ mirroring and stuff like that is done completely manually. So I have to make up a formula to calculate the new direction. I have the same basic idea, but I just can't think of a proper formula. Thanks for the response, though.
     
  4. Arras

    Arras GBAtemp Guru

    Member
    5,840
    2,659
    Sep 14, 2010
    Netherlands
    http://stackoverflow.com/questions/8954326/how-to-calculate-the-mirror-point-along-a-line
    As long as you use [ball location] + [speed vector] as your point, that should work. Just subtract the ball location from the result and you've got your mirrored vector.
     
  5. Kirito-kun

    Kirito-kun Disciple of GabeN

    Banned
    290
    98
    Jul 23, 2013
    Canada
    22nd Floor
    If you're serious about creating a game, why not use a game engine? You'd be able to create near retail-quality games with less work. Unity is a great engine to begin with. It makes you program game elements in C#, but if you know C++, transitioning to C# can be done with a few days of reading. The Unity SDK is free, as is the SDKs of many other game engines.
     
  6. ilman
    OP

    ilman Gbatemp's Official Noise Eraser

    Member
    1,130
    243
    Jul 25, 2010
    Shibuya
    I don't know. Pure-scripting a program just feels different than using a pre-made engine. I've had my fun with Game Maker Pro in the past, so I have something to base my opinion on. And I just find it a lot more entertaining to write everything down rather than clicking on certain commands and adding a little script inbetween. I hope you understand what I'm saying.
    Also, Arras, the link you posted here either doesn't explain it very well or I'm just being dumb. I mean where did all those letters pop out from? What is a, b, c and the d that I should calculate? It just isn't well explained.

    Edit: I just read the second answer (which I should've read before posting this) and everything is clear now. I'm going to try it out as soon as I get on my PC.
     
  7. Arras

    Arras GBAtemp Guru

    Member
    5,840
    2,659
    Sep 14, 2010
    Netherlands
    Basic maths. All linear equations can be converted to a form of ax+by+c=0 where a,b,c are constants. The formula -bx+ay+d=0 represents all lines that are perpendicular to it. You have the two points (center of ball and intersection point) so you can calculate a, b and c. Now you have the b and a for the second formula, so just substitute your point [ball location] + [speed vector] for x and y and the only unknown variable left is d. Now, calculate the intersection between the two lines (this shouldn't be hard either) and take "intersection + (intersection - [ball location + speed vector])" or something like that.
     
  8. ilman
    OP

    ilman Gbatemp's Official Noise Eraser

    Member
    1,130
    243
    Jul 25, 2010
    Shibuya
    Thanks, I already got it working. I appreciate the help. :D
     
  9. pyromaniac123

    pyromaniac123 ส็็็็็็็็็็็็็็็็็็็(ಠ益ಠส็็็็็็็็็็็็็็็็็็็

    Member
    2,001
    1,671
    Sep 24, 2011
    Giggity.