Monday 2 January 2012

Adding a player

The player is Just my UFO object that again extends from my base3Dobject. I've actually gone a little further here and extended it to a moveable3Dobject first as I may have other moveable objects that aren't player controlled.

The new moveable object has two key attributes:

* velocity - the direction the object is travelling in
* acceleration - the multiplier of how quickly to move in the direction of the velocity.

The update code of this object now updates the position of the object based on the set velocity multiplied by the acceleration. This will simply move the object around when I apply a velocity to it and as long as it is accelerating.

so now to add my player, I just call the same draw code and the UFO appears, doesn't do anything but it's on screen.

So in my player object that extends the moveable object, I need a method to take player input and change the velocity accordingly.

So far I'm just using the keyboard but it's easy to plug in the xbox controller code later on (with a nice input class I wrote for my 2D engine)

So if I press left or right arrow I update the velocity.x value to move the sprite across the x axis (width of the map).

If I press up or down arrow then I update the velocity.z to move through the Z axis (depth of the map).

So what about the Y axis? That's my altitude axis. I want the player to drop to the ground to simulate gravity unless they are using the thrust.

This is easily done by adding to velocity.Y if no thrust is applied so the UFO will fall.

If the player presses space then thrust is applied and I decrease velocity.y in increments of thrust values to give the impression that the UFO is slowly thrusting up against gravity.

So now when I run the game I can move the UFO aroud the world and it falls down unless I thrust.

So far so good, except when the UFO model gets to a a building object, it just passes straight through it. This is because the 3D objects are just pixels rendered to screen with a z buffer to hide objects blocked by other objects in the viewpoint.

They objects have no mass or physical being in the world.

This is exactly the same in a 2D game. Sprites will happily draw over each other. The solution is collision detection.

Same goes in a 3D world, I need to check if the objects in the game world are colliding and then act accordingly.

No comments:

Post a Comment