Where to start

Nov 30, 2009 at 5:31 PM

Hello.  I know how to programk in C# and XNA but I am not the best.  Im not asking for someone to write code for me, but I need help on where to put it.  I'm starting out simple and just making a ball that can roll around a plane with the arrow keys, but how would I go about that?   I would make a class for the ball and create a new class but where would I put the code that draws the ball?

Dec 1, 2009 at 5:50 AM

Hi mattloto, thanks for the interest in the project.  I'll get you started and you can ask question as we go.

The first thing to do is create a PhysicsComponent and add it to Game.Components collection.  Then in the Game.Update method, call the PhysicsComponent.EndUpdate() at the beginning of the method and call PhysicsComponent.BeginUpdate(GameTime) right before the base.Update(GameTime) call.  The physics are now ready to simulate.

Next, create the plane.  You need to create a CollisionGroup, add a CollisionPlane to it (CollisionGroup.CollisionShapes.Add()) and add the CollisionGroup to the PhysicsComponent.CollisionSystem.CollisionGroups collection.  That will set up the plane.

Next create the ball.  You need to create a RigidBody this time since the ball moves, add a CollisionSphere to it (RigidBody.CollisionGroup.CollisionShapes.Add()) and add the RigidBody to the PhysicsComponent.RigidBodies collection.

Do both of the above in the Game.Initialize method.  Then to move a ball around apply Torque to the ball via the RigidBody.ApplyTorque[Impulse] based on input.  This should happen between the PhysicsComponent.EndUpdate and BeginUpdate.

To draw the ball, Set the world transform to RigidBody.Orientation * Matrix.CreateTranslation(RigidBody.Position) and draw a ball model (or equivilent) of the correct size (CollisionSphere.Radius).

Of course, The ExampleScreen already has this functionality implemented...sort of.  Good luck!