Making objects bounce off eachother

Hi,

Im attempting to make asteroids bounce off eachother on collision. I have setup rectangles and tried to do this code:

1
2
3
4
5
Rectangle asteroidRectangle = new Rectangle((int)asteroids.Position.X, (int)asteroids.Position.Y, Asteroid.Texture.Width, Asteroid.Texture.Height);
                if (asteroidRectangle.Intersects(asteroidRectangle))
                {
                   // Not sure what to enter here
                }

The asteroid itself has a class which declares position, velocity, texture ect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 public Vector2 Position;
        public Vector2 Velocity;
        public static Texture2D Texture;
        public static Viewport GraphicsViewport;
        public bool alive = true;
        public Asteroid(Texture2D newTexture, Vector2 newPosition, Vector2 newVelocity)
        {
            Position = newPosition;
            Velocity = newVelocity;
        }

        public void Update()
        {
          
            Position = Position + Velocity;
            if (Position.X < 0 || Position.X > GraphicsViewport.Width - Texture.Width)
            {
                Velocity.X = -Velocity.X;
                Position.X = Position.X + Velocity.X;
            }

            if (Position.Y < 0 || Position.Y > GraphicsViewport.Height - Texture.Height)
            {
                Velocity.Y = -Velocity.Y;
                Position.Y = Position.Y + Velocity.Y;
          
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, Color.White);
        }       
    }


Please help!
I
Last edited on
Topic archived. No new replies allowed.