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

ilman

Gbatemp's Official Noise Eraser
OP
Member
Joined
Jul 25, 2010
Messages
1,128
Trophies
0
Age
25
Location
Shibuya
XP
570
Country
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.
 

Arras

Well-Known Member
Member
Joined
Sep 14, 2010
Messages
6,318
Trophies
2
XP
5,418
Country
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.
 

ilman

Gbatemp's Official Noise Eraser
OP
Member
Joined
Jul 25, 2010
Messages
1,128
Trophies
0
Age
25
Location
Shibuya
XP
570
Country
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.
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.

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.
 

Arras

Well-Known Member
Member
Joined
Sep 14, 2010
Messages
6,318
Trophies
2
XP
5,418
Country
Netherlands
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.
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.
 

Kirito-kun

Disciple of GabeN
Banned
Joined
Jul 23, 2013
Messages
290
Trophies
0
Location
22nd Floor
XP
165
Country
Canada
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.
 

ilman

Gbatemp's Official Noise Eraser
OP
Member
Joined
Jul 25, 2010
Messages
1,128
Trophies
0
Age
25
Location
Shibuya
XP
570
Country
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.
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.
 

Arras

Well-Known Member
Member
Joined
Sep 14, 2010
Messages
6,318
Trophies
2
XP
5,418
Country
Netherlands
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.
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.
 

ilman

Gbatemp's Official Noise Eraser
OP
Member
Joined
Jul 25, 2010
Messages
1,128
Trophies
0
Age
25
Location
Shibuya
XP
570
Country
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.

Thanks, I already got it working. I appreciate the help. :D
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Sonic Angel Knight @ Sonic Angel Knight: :ninja: