# HG changeset patch # User admin@yin.cr.ie.u-ryukyu.ac.jp # Date 1260867815 -32400 # Node ID 8ac82344727a8413c1ef60056867f2aff0da6d7b # Parent 8ab63ded2c3ff0ede473c4bc0d778349bb8c0e83 property_test add diff -r 8ab63ded2c3f -r 8ac82344727a Renderer/Test/property_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Test/property_test.cc Tue Dec 15 18:03:35 2009 +0900 @@ -0,0 +1,247 @@ +#include +#include +#include "SceneGraphRoot.h" +#include "SceneGraph.h" +#include "TaskManager.h" +#include "property_test.h" +#include "Func.h" + +ChainPropertyPtr properties[2]; +ChainProperty cv[CHAIN_LEN]; + + +void createSceneGraphFromProperty(SceneGraphPtr root, ChainPropertyPtr cv); + +void +init_chainold_vars(ChainPropertyPtr cv) { + cv->x = 0, cv->y = 0, cv->next_x = 0, cv->next_y = 0; + cv->vx = 0, cv->vy = 0, cv->next_vx = 0, cv->next_vy = 0; + cv->can_move = TRUE; +} + +void +set_old_vector(ChainPropertyPtr cv, SceneGraphPtr sg) { + sg->xyz[0] = (float)cv->next_x; + sg->xyz[1] = (float)cv->next_y; + sg->xyz[2] = 0.0f; +} + + +static void +chain_old_move_ope(SceneGraphPtr node, int screen_w, int screen_h) +{ + Pad *pad = sgroot->getController(); + + if (pad->cross.isHold()) { + cv[CHAIN_LEN-1].can_move = FALSE; + if (pad->left.isHold()) { + cv[CHAIN_LEN-1].x += -5.0; + } else if (pad->right.isHold()) { + cv[CHAIN_LEN-1].x += 5.0; + } + + if (pad->up.isHold()) { + cv[CHAIN_LEN-1].y += -5.0; + } else if (pad->down.isHold()) { + cv[CHAIN_LEN-1].y += 5.0; + } + } else { + cv[CHAIN_LEN-1].can_move = TRUE; + } +} +void +chain_old_move(ChainProperty cv, int w, int h) +{ +} +void +chain_old_move(SceneGraphPtr sg, int w, int h) +{ + int id = sg->id; + if(id == 0) { + for(int cnt = 0; cnt < 600; cnt++) { + for(int i = 0; i < CHAIN_LEN; i++) { + if(cv[i].can_move) { + double dx = cv[i-1].x - cv[i].x; + double dy = cv[i-1].y - cv[i].y; + double l = sqrt(dx * dx + dy * dy); + double a = k * (l - chain_width) / m; + double ax = a * dx / l; + double ay = a * dy / l; + if(i < CHAIN_LEN - 1) { + dx = cv[i+1].x - cv[i].x; + dy = cv[i+1].y - cv[i].y; + l = sqrt(dx * dx + dy * dy); + a = k * (l - chain_width) / m; + ax += a * dx / l; + ay += a * dy / l; + } + ay += g; + cv[i].vx *= safe; + cv[i].vy *= safe; + cv[i].next_vx = cv[i].vx + ax * dt; + cv[i].next_vy = cv[i].vy + ay * dt; + cv[i].next_x = cv[i].x + cv[i].vx * dt; + cv[i].next_y = cv[i].y + cv[i].vy * dt; + } else { + cv[i].next_x = cv[i].x; + cv[i].next_y = cv[i].y; + } + } + for(int i = 0; i < CHAIN_LEN; i++) { + cv[i].vx = cv[i].next_vx; + cv[i].vy = cv[i].next_vy; + cv[i].x = cv[i].next_x; + cv[i].y = cv[i].next_y; + } + } + // cout << id << ", " << sg->xyz[1] << endl; + } + set_old_vector(&cv[id], sg); + int p, n; + p = n = id; + if(p != 0) { + p--; + } + if(n != CHAIN_LEN - 1) { + n++; + } + sg->angle[2-(id%2)*2] + = 90 + atan((cv[p].next_y - cv[n].next_y) / (cv[p].next_x - cv[n].next_x)) * 180 / M_PI; +} + +/* +void +chain_old_collision(SceneGraphPtr sg, int w, int h, SceneGraphPtr osg) +{ + +} +*/ + +void +spe_move_collision(ChainProperty cv, spe_move_func new_move, spe_move_func new_collision) +{ + + cv.move = new_move; + cv.collision = new_collision; + +} + + +void +chain_old_collision(void *cv, int w, int h, ChainPropertyPtr ocv) +{ + //createSceneGraphFromProperty(cv, cv); +} + + +void +property_swich() +{ + +} + + +void +createSceneGraphFromProperty(SceneGraphPtr root, ChainPropertyPtr cv) +{ + SceneGraphPtr node; + + for(int i = 0; i < CHAIN_LEN; i++) { + node = sgroot->createSceneGraph(cv[i].objname); + /** + * move, collision は spe で実行される, task taskID を持たせればよい + * property が 対応する taskID を持つので set_move_collision() は不要 + * set_task_propety(move_taskID, collision_taskID) のようなものを作る + */ + node->set_move_collision(chain_old_move, chain_old_collision); + //spe_move_collision(cv[i], chain_old_move, chain_old_collision); + node->id = cv[i].id; + node->angle[1] = cv[i].angle[1]; + set_old_vector(&cv[i], node); + cv[i].parent->addChild(node); + } + sgroot->setSceneData(root); +} + + +void +set_properties(ChainPropertyPtr cv) +{ + properties[0] = cv; + properties[1] = cv; +} + +MainLoopPtr +Chain::init(Viewer *sgroot, int w, int h) +{ + SceneGraphPtr root, chain; + sgroot->createFromXMLfile("xml_file/chain.xml"); + + ChainProperty rcv; + init_chainold_vars(&rcv); + rcv.next_x = w / 2; + rcv.next_y = 0.0; + rcv.id = CHAIN_LEN; + + root = sgroot->createSceneGraph("CHAIN"); + root->set_move_collision(chain_old_move_ope, chain_old_collision); + set_old_vector(&rcv, root); + + chain = sgroot->createSceneGraph("CHAIN"); + chain->set_move_collision(chain_old_move, chain_old_collision); + + set_properties(cv); + + for(int i = 0; i < CHAIN_LEN; i++) { + init_chainold_vars(&cv[i]); + cv[i].x = 0; + cv[i].y = chain_width * i; + cv[i].angle[1] = -90 * (i % 2); + + chain->id = cv[i].id; + chain->angle[1] = cv[i].angle[1]; + set_old_vector(&cv[i], chain); + + cv[i].root = root; + cv[i].objname = "CHAIN"; + cv[i].id = i; + + cv[i].parent = root; + + } + + createSceneGraphFromProperty(root, cv); + cv[0].can_move = FALSE; + //sgroot->setSceneData(root); + + return sgroot; +} + +extern Application * +application() { + return new Chain(); +} + +const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; + +extern int init(TaskManager *manager, int argc, char *argv[]); +extern void task_initialize(); +static void TMend(TaskManager *manager); + +int +TMmain(TaskManager *manager, int argc, char *argv[]) +{ + task_initialize(); + manager->set_TMend(TMend); + return init(manager, argc, argv); + +} + +void +TMend(TaskManager *manager) +{ + printf("test_nogl end\n"); +} + +/* end */ + diff -r 8ab63ded2c3f -r 8ac82344727a Renderer/Test/property_test.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Test/property_test.h Tue Dec 15 18:03:35 2009 +0900 @@ -0,0 +1,47 @@ +#include "Application.h" + +typedef void (*spe_move_func)(void *cv, int screen_w, int screen_h); +typedef void (*spe_collision_func)(void *cv, int screen_w, int screen_h); + +typedef struct { + double x, y, next_x, next_y; + double vx, vy, next_vx, next_vy; + double angle[3]; + int can_move; + int id; + char *objname; + spe_move_func move; + spe_collision_func collision; + SceneGraphPtr parent; + SceneGraphPtr root; +} *ChainPropertyPtr, ChainProperty; + +#define FALSE 0 +#define TRUE !FALSE +#define CHAIN_LEN 50 + +static const double m = 100.0; +static const double k = 7000.0; +static const double g = 9.8; +static const double dt = 0.003; +static const double chain_width = 10; +static const double safe = 0.995; + +class Chain : public Application { +public: + Chain(){} + + SceneGraphPtr scenegraph_factory(void *p, int size); + SceneGraphPtr scenegraph_connector(void *p, int size, SceneGraphPtr s, SceneGraphPtr sg_available_list); + void init(TaskManager *manager, int w, int h); + void init_chain_vars(ChainPropertyPtr cv); + void chain_move(TaskManager *manager, SceneGraphPtr sg, int w, int h); + //void chain_collision(SceneGraphPtr sg, int w, int h, SceneGraphPtr osg); + void chain_collision(ChainPropertyPtr cv, int w, int h); + void createSceneGraphFromProperty(SceneGraphPtr root, ChainPropertyPtr cv); + //void property_swich(); + //void set_properties(ChainPropertyPtr cv); + MainLoopPtr init(Viewer *viewer, int screen_w, int screen_h); + + +};