Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Implementation of Race Game Tree

Tags:

c++

tree

opengl

I am building a racing game in OpenGL using Glut, and I'm a bit lost in the details. First of all, any suggestions or a road map would be more than great.

So far what I am thinking is this:

  1. Tree implementation for transformations.
  2. Simulated dynamics.(*)
  3. Octree implementation for collision detection.
  4. Actual collision detection.(*)
  5. Modelling in Maya and export them as .OBJs.
  6. Polishing the game with GLSL or something like that for graphics quality.

(*): I am not sure the order of these two.

So I started with the simulated dynamics without a tree implementation, and it turned out to be a huge chaos for me. Is there any way you can think of something that could help me to build such a tree to use in racing game?

I thought of something like this but I have no idea how to implement it.

Reds are static, yellows are dynamic nodesReds are static, yellows are dynamic nodes

like image 558
hevele Avatar asked Dec 11 '12 21:12

hevele


2 Answers

I would suggest the exact opposite of @bezad.

Start with a single car and an infinite road.

Split the problem of rendering and dynamics into two completely different things. The common Car updates and/or is the link between the CarRenderModel and the CarPhysicsModel.

What shapes the Car puts into the GL scene is up to the Car.

Among other things, this means you can have a really simple Car show up on screen, and attach a really simple physics model to it, and either make the Car prettier or make it behave physically better without having to tie the two together. And, ideally, at each stage you have something playable.

So, a car that is a rectangle, 5 long 3 wide and 1 unit high. A road that is 13 units wide, and goes on forever. A stationary camera. Maybe a horizon. The first physics model is a rocket ship, where every second you push down on an arrow key the car gains x units/second of velocity in that direction. Note that this car doesn't rotate -- it is axis aligned. If the car leaves the road, it explodes, and the "game" ends.

You now have something on the screen that responds to user input. You could spend time making a fancier car model (wheels, etc), or you could improve the car physics and control model (direction! Angle! Breaking != speeding up!), or you could make the environment more interesting (add black-and-white stripes so you can see velocity at the edge of the road. An off-road portion near the road, and maybe trees that blow up the car), or you could make the camera more interesting (say, it stays behind the car, and looks over its shoulder).

Now, for dynamics, I'd treat universe-car interaction using distinct code from car-car interaction, just to keep my sanity intact. The car doesn't get to modify the environment.

This means you can write a bunch of the car-universe interaction easier than you can the car-car interaction.

...

Building an arbitrary tree in C++ is easy.

#include <vector>
#include <memory>
#include <string>
struct MyTree;
typedef std::unique_ptr<MyTree> upTree; // punt on memory management!
struct MyBaseNode;
typedef std::unique_ptr<MyBaseNode> upNode;

struct MyTree {
  std::vector<upTree> children;
  upNode node;
  MyTree( upNode node_ ):node(std::move(node_)) {}
private:
// if C++11 compiler, use these:
  MyTree( MyTree const& ) = delete;
  MyTree& operator=( MyTree const& ) = delete;
// if C++03, use these:
  // MyTree( MyTree const& ); // no implementation
  // MyTree& operator=( MyTree const& ); // no implementation
};
upTree make_tree(upNode node) { return upTree( new MyTree(std::move(node)) ); }

enum EOrder{ eFirst, eMiddle, eLast };
template<typename Functor>
void walk_tree( upTree const& tree, Functor f, bool bFirst = true, bool bLast = true) {
  if (!tree) return;
  f( tree, bFirst, bLast );
  for (auto it = tree->children.begin(); it != tree->children.end(); ++it) {
    bool bChildFirst = (it == tree->children.begin());
    bool bChildLast = ((it+1) == tree->children.end());
    walk_tree( *it, f, bChildFirst, bChildLast );
  }
}
struct MyBaseNode {
  virtual ~MyBaseNode() {};
  // put things that your tree nodes have to be able to do here
  // as pure virtual functions...
  virtual std::string MyName() const = 0;
};

struct CarsNode : MyBaseNode {
  // cars node implementation!
  virtual std::string MyName() const /*override*/ { return "I am a bunch of CARS!"; }
};
upNode make_cars() { return upNode( new CarsNode() ); }

struct CarNode : MyBaseNode {
  // car node implementation!
  virtual std::string MyName() const /*override*/ { return "I am a CAR!"; }
};
upNode make_car() { return upNode( new CarNode() ); }

struct RoadNode : MyBaseNode {
  // car node implementation!
  virtual std::string MyName() const /*override*/ { return "I am a ROAD!"; }
};
upNode make_road() { return upNode( new RoadNode() ); }

#include <iostream>
void tree_printer_func( upTree const& tree, bool bFirst, bool bLast ) {
  if (bFirst) std::cout << "[ ";
  if (tree->node) {
    std::cout << tree->node->MyName().c_str();
  } else {
    std::cout << "nullNode";
  }
  if (bLast) {
    std::cout << " ]\n";
  } else {
    std::cout << ", ";
  }
}
int main() {
  upTree root = make_tree(upNode());
  upTree carsTree = make_tree(make_cars());
  carsTree->children.push_back( make_tree( make_car() ) );
  carsTree->children.push_back( make_tree( make_car() ) );
  root->children.push_back( std::move(carsTree) );
  upTree roadTree = make_tree(make_road());
  root->children.push_back( std::move(roadTree) );
  walk_tree( root, tree_printer_func );
}

the above is pretty rough (I didn't get end-node handling quite right in the printer, for example), but it demonstrates a non-homogenious, non-leaking, n-ary tree structure in C++.

like image 97
Yakk - Adam Nevraumont Avatar answered Oct 08 '22 13:10

Yakk - Adam Nevraumont


If I understand you right, you are asking for ideas on how to go about developing the project (your game). I would start with the smaller objects like the tire, body, other car parts and the car, then the pieces of the environment, the terrain and road track etc. Finally the world and putting all other objects together into the world. Hope it helps.

like image 45
Baz Avatar answered Oct 08 '22 11:10

Baz