C++实现种族游戏树

hev*_*ele 9 c++ opengl tree

我正在使用Glut在OpenGL中构建一个赛车游戏,我在细节上有点迷失.首先,任何建议或路线图都会非常棒.

到目前为止我的想法是这样的:

  1. 转换的树实现.
  2. 模拟动力学.(*)
  3. 八叉树实施碰撞检测.
  4. 实际碰撞检测.(*)
  5. 在Maya中建模并将它们导出为.OBJ.
  6. 使用GLSL或类似图形质量的东西来抛光游戏.

(*):我不确定这两个的顺序.

所以我从没有树实现的模拟动态开始,结果对我来说是一个巨大的混乱.有什么方法你可以想到一些可以帮助我建立这样一棵树用于赛车游戏的东西吗?

我想到了类似的东西,但我不知道如何实现它.

红色是静态的,黄色是动态节点 红色是静态的,黄色是动态节点

Yak*_*ont 10

我建议与@bezad完全相反.

从一辆车和一条无限的道路开始.

将渲染和动态问题分成两个完全不同的东西.常见的Car更新和/或是CarRenderModel和之间的联系CarPhysicsModel.

Car投入GL场景的形状取决于Car.

除此之外,这意味着您可以Car在屏幕上进行非常简单的显示,并为其添加一个非常简单的物理模型,或者使其Car更漂亮或使其表现得更好,而不必将两者结合在一起.理想情况下,在每个阶段你都有可玩的东西.

所以,一辆长方形的汽车,5长3宽1高单位.一条宽13个单位的道路,永远持续下去.固定的相机.也许是一个视野.第一个物理模型是火箭飞船,每按一下箭头键,汽车就会在那个方向上获得x单位/秒的速度.请注意,此车不会旋转 - 它是轴对齐的.如果汽车离开道路,它会爆炸,"游戏"结束.

您现在在屏幕上有一些响应用户输入的内容.你可以花时间制作一个更漂亮的汽车模型(车轮等),或者你可以改进汽车物理和控制模型(方向!角度!打破!=加速!),或者你可以使环境更有趣(添加黑色 - 白色条纹让你可以看到道路边缘的速度.道路附近的越野部分,也许树木会炸毁汽车),或者你可以让相机更有趣(比如,它留在汽车,看着它的肩膀).

现在,对于动态,我会使用与汽车互动不同的代码来对待宇宙车交互,只是为了保持我的理智不变.汽车无法改变环境.

这意味着你可以比汽车互动更容易地写出一堆汽车宇宙互动.

...

用C++构建任意树很容易.

#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 );
}
Run Code Online (Sandbox Code Playgroud)

以上是非常粗糙的(例如,我没有在打印机中完全正确地处理端节点处理),但它演示了C++中非同质,非泄漏,n-ary树结构.