You Like My Tutorials? Click The Banner To Keep 'Em Coming!

glGameDeveloper(): The Game Developer's Source for OpenGL Coding
Main Menu
Latest News
Archived News
Tutorials
Links
E-Mail Me

Other
Message Board
My Guestbook

Tutorials -- Working with Objects
 


Working with Objects Using Classes Page 3 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

The BasicObject Class

Here's the code to define the class and also has comments for each of the functions included with it. You can define it anywhere in your code, but I recommend adding a C++ header file called "classes.h" to your project and putting it in there.

Move on to the next section to see how we would make a subclass of this class.

/**********************************************************
BasicObject Class:
    
    This class contains the basic stuff for an object: 
    position, size, velocity, and boundary coordinates. 
    
    SetTimeInt(float t) - sets the time interval to update 
    the object according to how much time has passed since 
    the last update.

    UpdateSides() - updates the side coordinate values, 
    used for collision detection.

    UpdateAcc() - updates the velocity based on the 
    acceleration values

    UpdatePos() - updates the new position based on 
    velocity and the amount of time passed.

    SetPos(float nx, float ny, float nz) - sets new 
    position.

    SetVel(float nvx, float nvy, float nvz) - sets new 
    velocity

    SetAcc(float nax, float nay, float naz) - sets new 
    acceleration

    SetSize(float nw, float nl, float nh) - sets new size and 
    updates the new side coordinates


**********************************************************/

class BasicObject {
public:
    float x, y, z;      // position variables
    float vx, vy, vz;   // velocity variables
    float ax, ay, az;   // acceleration variables
    float l, w, h;      // size variables
    
    // side boundary coordinate variables
    float left, rght, frnt, back, top, btm;
    float time;

    void SetTimeInt(float t) {
        time = t;
    }
    void UpdatePos() {
        x += vx * time;
        y += vy * time;
        z += vz * time;
    }
    void UpdateAcc() {
        vx += ax;
        vy += ay;
        vz += az;
    }
    void UpdateSides() {
        left = x - w;
        rght = x + w;
        frnt = z + l;
        back = z - l;
        top  = y + h;
        btm  = y - h;
    }
    void SetPos(float nx, float ny, float nz) {
        x = nx;
        y = ny;
        z = nz;
        UpdateSides();
    }
    void SetVel(float nvx, float nvy, float nvz) {
        vx = nvx;
        vy = nvy;
        vz = nvz;
    }
    void SetAcc(float nax, float nay, float naz) {
        ax = nax;
        ay = nay;
        az = naz;
    }
    void SetSize(float nw, float nl, float nh) {
        w = nw;
        l = nl;
        h = nh;
        UpdateSides();
    }
};


That's it. Move on to the next section to see how we would make a subclass of this class.

Working with Objects Using Classes Page 3 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

This site best viewed with MS Internet Explorer 5.0 at 16-bit color and at least 800x600.

All Contents of this site are Copyright 1999, 2000 Joe Zimmerman. No part of this site (code, HTML, etc.) may be reproduced without my permission.

Click Here!