/// <summary> /// The location is first saved and the model is translated /// to the origin before any rotations are applied. Objects rotate about their /// center. After rotations, the location is reset and updated iff it is not /// outside the range of the stage (stage.withinRange(String name, Vector3 location)). /// When movement would exceed the stage's boundaries the model is not moved /// and a message is displayed. /// /// AGMGSK is a terrain based 3D environment, so rotations are on Y. /// Therefore an Euler rotation method is adopted below. MonoGames supports both /// Matrix.CreateFromAxisAngle(...) and CreateFromQuaternion(...) rotations /// for more complex 3D environments. /// </summary> public void updateMovableObject() { Vector3 startLocation, stopLocation; startLocation = stopLocation = Translation; // set for current location Orientation *= Matrix.CreateTranslation(-1 * Translation); // move to origin // Euler rotations Orientation *= Matrix.CreateRotationY(yaw); // rotate Orientation *= Matrix.CreateRotationX(pitch); Orientation *= Matrix.CreateRotationZ(roll); stopLocation += ((step * stepSize) * Forward); // move forward // assume no collision possible if moving off terrain if (stage.withinRange(this.Name, stopLocation)) // on terrain, check collision { if (model.IsCollidable && collision(stopLocation)) // collision { Orientation *= Matrix.CreateTranslation(startLocation); // restore position } else // no collision and on suface move { Orientation *= Matrix.CreateTranslation(stopLocation); // move forward } } else // off terrain, don't move { Orientation *= Matrix.CreateTranslation(startLocation); // restore position } }
/// <summary> /// The location is first saved and the model is translated /// to the origin before any rotations are applied. Objects rotate about their /// center. After rotations, the location is reset and updated iff it is not /// outside the range of the stage (stage.withinRange(String name, Vector3 location)). /// When movement would exceed the stage's boundaries the model is not moved /// and a message is displayed. /// /// AGMGSK is a terrain based 3D environment, so rotations are on Y. /// Therefore an Euler rotation method is adopted below. MonoGames supports both /// Matrix.CreateFromAxisAngle(...) and CreateFromQuaternion(...) rotations /// for more complex 3D environments. /// </summary> /// // Modded to return the object it is colliding with, if no collision return null public Object3D updateMovableObject() { Object3D obj3d = null; Vector3 startLocation, stopLocation; startLocation = stopLocation = Translation; // set for current location Orientation *= Matrix.CreateTranslation(-1 * Translation); // move to origin // Euler rotations Orientation *= Matrix.CreateRotationY(yaw); // rotate Orientation *= Matrix.CreateRotationX(pitch); Orientation *= Matrix.CreateRotationZ(roll); stopLocation += ((step * stepSize) * Forward); // move forward // assume no collision possible if moving off terrain if (stage.withinRange(this.Name, stopLocation)) // on terrain, check collision { obj3d = CollidedWith(stopLocation); if (model.IsCollidable && Collision(obj3d)) // collision ==> code left similar for clarity { Orientation *= Matrix.CreateTranslation(startLocation); return(obj3d); } // restore position else // no collision and on suface move { Orientation *= Matrix.CreateTranslation(stopLocation); // move forward } } else // off terrain, don't move { Orientation *= Matrix.CreateTranslation(startLocation); // restore position } return(null); }