view Renderer/Test_/ball_bound.cc @ 4:b5b462ac9b3b

Cerium Blender ball_bound
author Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp>
date Mon, 29 Nov 2010 16:42:42 +0900
parents
children 8f445f1bfe66
line wrap: on
line source

#include <stdlib.h>
#include <math.h>
#include "SceneGraphRoot.h"
#include "Application.h"
#include "MainLoop.h"


// prototype
int TMmain(TaskManager *manager, int argc, char *argv[]);
extern int init(TaskManager *manager, int argc, char *argv[]);
extern void task_initialize();
extern Application* application();
static void cube_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
static void cube_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree);
static void cone_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
static void cone_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree);
static void cube_move_idle(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
static void cube_collision_idle(SceneGraphPtr, void *sgroot_, int w, int h, SceneGraphPtr tree);

static void TMend(TaskManager *manager);




static float vy = 0.0f; // y 方向速度
static float dt = 1.0/1.0f; // frame rate 
static float e = -0.8f;  // 反発係数
static float g = 9.8f;  // 重力加速度
static float w0 = 320; // 初期x
static float h0 = -1000; // 初期y
static float d0 = 30.0f; // 初期z
static float cube_radius = 10.0f; // cube半径
static int time = 0; // 停止してる時間
const char *usr_help_str = "Usage: ./cube_bound [OPTION]\n";
static int speed = 5; // 自機のスピード


// init
class cube_bound : public Application{
    MainLoopPtr init(Viewer *viewer, int screen_w, int screen_h);
};
MainLoopPtr cube_bound::init(Viewer *sgroot, int screen_w, int screen_h){
	
	// 乱数初期化
	srandom(100);
	
	// rootの作成
	SceneGraphPtr root = sgroot->createSceneGraph();



	// 各xmlから読み込んで初期位置を決める
	SceneGraphPtr cube;
	SceneGraphPtr cone;
	sgroot->createFromXMLfile("xml_file/cube.xml");
	sgroot->createFromXMLfile("xml_file/cone.xml");
	cube = sgroot->createSceneGraph("Cube");
	cone = sgroot->createSceneGraph("Cone");
	cube->xyz[0] = w0;
	cube->xyz[1] = h0;
	cube->xyz[2] = d0;
	cone->xyz[0] = screen_w/2;
	cone->xyz[1] = screen_h/2;
	cone->xyz[2] = d0;

	// 各オブジェクトの動きを設定
	cube->set_move_collision(cube_move, cube_collision);
	cone->set_move_collision(cone_move, cone_collision);
	
	//それぞれをrootの子に入れてSceneGraphを作る
	root->addChild(cube);
	root->addChild(cone);
	sgroot->setSceneData(root);

	return sgroot;
}




// TMmain
int TMmain(TaskManager *manager, int argc, char *argv[]) {
	task_initialize();
	manager->set_TMend(TMend);
	return init(manager, argc, argv);
}

// Applicatinとして呼ばれた場合 cube_boundを実行
extern Application* application() {
	return new cube_bound();
}

// cubeの動き 落下する動き
static void cube_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) {
	vy += g * dt;
	node->xyz[1] += vy * dt;
}

// cubeのcollision screen_hで跳ね返る.止まるとmove_idelに切り替える
static void cube_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h,SceneGraphPtr tree){
	if (node->xyz[1] > screen_h - cube_radius) {
		node->xyz[1] = screen_h - cube_radius;
		vy *= e;
		if (vy > -g && vy < 0) {
			vy = 0.0;
			node->set_move_collision(cube_move_idle, cube_collision_idle);
		}
	}
}



// coneのmove パッドに合わせて回転しながら移動
static void cone_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) {
	SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
	Pad *pad = sgroot->getController();
	if (pad->left.isPush() || pad->left.isHold()) {
		node->xyz[0] -= speed;
		node->angle[1] += 10;
	}else if (pad->right.isPush() || pad->right.isHold()) {
		node->xyz[0] += speed;
		node->angle[1] -= 10;
	}
	if (pad->up.isPush() || pad->up.isHold()) {
		node->xyz[1] -= speed;
		node->angle[0] += 10;
	}else if (pad->down.isPush() || pad->down.isHold()) {
		node->xyz[1] += speed;
		node->angle[0] -= 10;
	}
}

// coneのcollision なし
static void cone_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h,SceneGraphPtr tree){
}




// cubeのmove(idle) 40フレーム待ってcube_moveに戻る
static void cube_move_idle(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h){
	time++;
	if (time > 40) {
		float w = (float)random();
		w = fmodf(w, screen_w - cube_radius*2);
		node->xyz[0] = w + cube_radius;
		node->xyz[1] = h0;
		node->xyz[1] = d0;
		node->set_move_collision(cube_move, cube_collision);
		time = 0;
	}
}

// cubeのcollision(idle) なし
static void cube_collision_idle(SceneGraphPtr, void *sgroot_, int w, int h, SceneGraphPtr tree){
}

// TMend
void TMend(TaskManager *manager){
	printf("test_nogl end\n");
}