currently placed. Near the end of the method, we use a helper function to update the acceleration values in light of collisions. We will now implement this helper method, but first we introduce another method. Add the following method to the Marble class: C# protected Axis CollideDirection(Triangle collideTriangle) { if (collideTriangle.A.Z == collideTriangle.B.Z && collideTriangle.B.Z == collideTriangle.C.Z) { return Axis.Z; } else if (collideTriangle.A.X == collideTriangle.B.X && collideTriangle.B.X == collideTriangle.C.X) { return Axis.X; } else if (collideTriangle.A.Y == collideTriangle.B.Y && collideTriangle.B.Y == collideTriangle.C.Y) { return Axis.Y; } return Axis.X | Axis.Z; } This method simply inspects a triangle’s points in order to determine the plane it is on and returns the axis that is perpendicular to it. Now add the following method to the Marble class, which will alter acceleration in light of collisions: C# protected void UpdateWallCollisionAcceleration(IEnumerable<Triangle> wallTriangles) { foreach (var triangle in wallTriangles) { Axis direction = CollideDirection(triangle); // Decrease the acceleration in x-axis of the component if ((direction & Axis.X) == Axis.X) { if (Velocity.X > 0) Acceleration.X -= wallFriction; else if (Velocity.X < 0) Acceleration.X += wallFriction; }
currently placed. Near the end of the method, we use a helper function to update the acceleration values in light of collisions. We will now implement this helper method, but first we introduce another method. Add the following method to the Marble class: C# protected Axis CollideDirection(Triangle collideTriangle) { if (collideTriangle.A.Z == collideTriangle.B.Z && collideTriangle.B.Z == collideTriangle.C.Z) { return Axis.Z; } else if (collideTriangle.A.X == collideTriangle.B.X && collideTriangle.B.X == collideTriangle.C.X) { return Axis.X; } else if (collideTriangle.A.Y == collideTriangle.B.Y && collideTriangle.B.Y == collideTriangle.C.Y) { return Axis.Y; } return Axis.X | Axis.Z; } This method simply inspects a triangle’s points in order to determine the plane it is on and returns the axis that is perpendicular to it. Now add the following method to the Marble class, which will alter acceleration in light of collisions: C# protected void UpdateWallCollisionAcceleration(IEnumerable<Triangle> wallTriangles) { foreach (var triangle in wallTriangles) { Axis direction = CollideDirection(triangle); // Decrease the acceleration in x-axis of the component if ((direction & Axis.X) == Axis.X) { if (Velocity.X > 0) Acceleration.X -= wallFriction; else if (Velocity.X < 0) Acceleration.X += wallFriction; }
3DGameDevelopmentWithXNA.docx