Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075929 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:00:54 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Need help understanding this bouncing collision
Pages: [1]
Print
Author Topic: Need help understanding this bouncing collision  (Read 385 times)
Parrotrd
Level 0
**


View Profile
« on: September 15, 2011, 04:47:53 AM »

Hey guys I'm learning C# XNA from a book I purchased, I'm making a space shooter at the moment and I can't understand this bouncing collision for the asteroids:

Code:
private void BounceAsteroids(Sprite asteroid1, Sprite asteroid2)
            {
                {
                    Vector2 cOfMass = (asteroid1.Velocity +
                        asteroid2.Velocity) / 2;

                    Vector2 normal1 = asteroid2.Center - asteroid1.Center;
                    normal1.Normalize();
                    Vector2 normal2 = asteroid1.Center - asteroid2.Center;
                    normal2.Normalize();

                    asteroid1.Velocity -= cOfMass;
                    asteroid1.Velocity =
                        Vector2.Reflect(asteroid1.Velocity, normal1);
                    asteroid1.Velocity += cOfMass;

                    asteroid2.Velocity -= cOfMass;
                    asteroid2.Velocity =
                        Vector2.Reflect(asteroid2.Velocity, normal2);
                    asteroid2.Velocity += cOfMass;
                }
            }

I've read the description in the book which tells me what the code does, I understand that and it works in the game but I can't calculate it myself so I don't understand what's happening.

If someone could give me an example using actual numbers instead of these variables I'd really appreciate it!
Logged
increpare
Guest
« Reply #1 on: September 15, 2011, 04:59:31 AM »

If you can't calculate it yourself, you don't understand it.

You know how conservation of momentum works?

You understand what all the methods being called in that routine are doing?

"If someone could give me an example using actual numbers instead of these variables I'd really appreciate it!"
Why can't you substitute in numbers yourself?
Logged
st33d
Guest
« Reply #2 on: September 15, 2011, 07:34:01 AM »

What's wrong with attaching some text to the asteroids and having them tell you what the values are doing?

The most important debugging and learning skill you have to hand in programming is getting the code to draw a diagram for you.

Understanding it comes second.
Logged
Parrotrd
Level 0
**


View Profile
« Reply #3 on: September 15, 2011, 08:30:49 AM »

What's wrong with attaching some text to the asteroids and having them tell you what the values are doing?

The most important debugging and learning skill you have to hand in programming is getting the code to draw a diagram for you.

Understanding it comes second.

Lol I can't believe I forgot that, I did that all the time when I was doing c++. Thanks a ton I can understand it now. Tongue
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic