Book review: Papervision 3D Essentials (follow link to buy)
ISBN 1847195725
ISBN 13 978-1-847195-72-2
Publisher: Packt Publishing
Author: Paul Tondeur and Jeff Winder.
This review here is long overdue, but since work got a bit hectic, and I spent some time moving it got pushed back.

Papervision3D Essentials Cover

Papervision3D Essentials Cover

Short version: If you want to get started with Papervision 3D, get this book. It’s a well written, great introduction for people new to Papervision3D, as well as a good reference for more experienced developers. It covers everything from basics to filters, effects and performance topics.

Longer version: Before I started reading this book I had never used Pv3D except looking at some neat demos. I had some experience with OpenGL from way-back, so the basic 3d concepts were not new to me.  As I got started I found that most of what I knew from general 3d graphics development was covered pretty well in the first few chapters. From the basic building blocks, to coordinate systems and primitives. The book starts out with a chapter dedicated to setting up authoring tools for compiling the code created during the book. Both Flash CS3/CS4 and Flex/Flash Builder is covered. After that it deals briefly with some core AS3 concepts before moving on to fundamental 3D subjects. Everything is explained really well, and as the book progresses trough the rest of the chapters it details subjects as drawing primitives, adding materials, setting up cameras and loading external models, then going on with particle systems, external models, filters and effects. A chapter is also dedicated to performance optimization. The book offers a number of good examples, also available on-line.

The authors are clearly very comfortable with the subjects they are covering, and discussing pretty much every topic in a language that makes it easy to follow, also for less experienced developers. In my opinion this is a very well written book that I would not hesitate to recommend to beginning developers as well as experienced.

As I mentioned in this previous post I’ve started to play around with C# and XNA Game Studio, and it’s very entertaining. I started out using The Platformer Starter Kit , which is a basic platformer game that you can customize and base your game on. I followed the tutorials mentioned in the previous link, and everything just works pretty smooth. Very satisfying to see immediate results reflected in a working game. After finishing them I decided to add a new feature by myself, a falling apple which would kill the player (I’ve been playing I Wanna Be The Guy lately). Hence, this post: XNA Platformer Starter Kit – Falling Apples. Now, this is a very basic feature. I just based my Apple on a copy of the Gem class, and did some minor modifications to allow them to fall down once the player walks under them, and if the player is hit he’ll die. A picture to show the final result:

Falling Apples

Falling Apples

Please bear in mind that this post is just a note of the changes I did to make this work, and should not be considered a full tutorial. I might end up doing more proper posts on XNA as I progress in my work with it.

So, here it goes!

First, create a new C# class. Just copy all the contents from the Gem class, and rename Gem to Apple (and gems to apples). Also, add the following line to the Apple constructor so that we can see the difference between apples and gems: Color = Color.Red;

Now, remove the following lines, since we do not need them:

private SoundEffect collectedSound;
 
public readonly int PointValue;

And add these:

private const float GravityAcceleration = 6500.0f;
private const float MaxFallSpeed = 600.0f;
private bool isFalling;
 
public Vector2 Velocity
{
    get { return velocity; }
    set { velocity = value; }
}
Vector2 velocity;

Now, since we want to be able to trigger the apples to fall when the player passes under them, we’ll need to create a bounding rectangle, which is taller than the actual apple. This will be the rectangle the player will collide with once he passes under the apple, and hence the apple will start falling.

public Rectangle TriggerRectangle
{
    get
    {
        int left = (int)basePosition.X;
        int width = 32;
        int top = (int)basePosition.Y;
        int height = 200;
        return new Rectangle(left, top, width, height);
    }
}

We need to implement some kind of basic physics to calculate how fast the apple should be falling. This is done by adding a new method called ApplyPhysics, which checks if the apple is falling, and if it is, it updates the velocity and position based on some simple calculations:

private void ApplyPhysics(GameTime gameTime)
{
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    if (isFalling)
    {
        velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * elapsed, -MaxFallSpeed, MaxFallSpeed);
        Position += velocity * elapsed;
    }
}

To make sure it actually falls when triggered we need to update public void Update(GameTime gameTime) and add a call to the ApplyPhysics method we just created. The call can be added right after bounce, at the end of the method:

    bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;
    ApplyPhysics(gameTime);
}

The last thing we need to do in the Apple class is to add a method for actually starting the fall:

public void OnRockFalling()
{
    isFalling = true;
}

Next up are some changes to the Level class. Find the line which creates the Gem array:

private List<Gem> gems = new List<Gem>();

And add a new array for the Apples:

private List<Apple> apples = new List<Apple>();

Next up is the loading of the Apples. In the LoadTile method, add a new case statement as follows:

    case 'R':
        return LoadAppleTile(x, y);

This will load R’s in the level code as apples. We now need to create the LoadAppleTile method:

private Tile LoadApppleTile(int x, int y)
{
    Point position = GetBounds(x, y).Center;
    apples.Add(new Apple(this, new Vector2(position.X, position.Y)));
    return new Tile(null, TileCollision.Passable);
}

Almost there! We need to create a few news method for getting the apples to work. The first is called UpdateApples, which checks the collisions with the player:

private void UpdateApples(GameTime gameTime)
{
    for (int i = 0; i < apples.Count; ++i)
    {
        Apple apple = apples[i];
        apple.Update(gameTime);
        if (apple.BoundingCircle.Intersects(Player.BoundingRectangle))
        {
            OnPlayerKilled(null);
        }
        else if (apple.TriggerRectangle.Intersects(Player.BoundingRectangle))
        {
            OnAppleFalling(apple);
        }
    }
}

Next up is the OnAppleFalling method, which in turn calls OnAppleFalling in the Apple class.

private void OnAppleFalling(Apple apple)
{
    apple.OnAppleFalling();
}

Now we just need to insert calls for these methods in the correct places. First up is the UpdateApples method. This is called from the Update method. Just find the following line:

    UpdateGems(gameTime);

and insert the UpdateApples call under it:

    UpdateApples(gameTime);

The last change is to the Draw method. You’ll see there is two lines used to draw the gems, and that’s what we need to do for our apples. Insert the two following lines after the code for the gems:

foreach (Apple apple in apples)
    apple.Draw(gameTime, spriteBatch);

Now, to give this a spin, open the 0.txt level file and place some R’s here and there. When you start up the game you should see some red gems which falls down when you pass under them.

PS: A more clever approach here would be to create a base class for these classes, which would hold all the shared logic and methods, but the goal for this tutorial is to show how to implement the new feature as easily as possible.

[tags]C#,XNA,Game Development[/tags]