Mercurial > hg > Game > Cerium
changeset 375:2ae25549dd6a draft
chain.cpp added
author | kazz@kazzone.ie.u-ryukyu.ac.jp |
---|---|
date | Wed, 06 May 2009 15:51:42 +0900 |
parents | 4fad8f9329ac |
children | 4fb99cd5d13f |
files | TaskManager/Test/test_render/chain.cpp |
diffstat | 1 files changed, 142 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TaskManager/Test/test_render/chain.cpp Wed May 06 15:51:42 2009 +0900 @@ -0,0 +1,142 @@ +#include <iostream.h> +#include <math.h> +#include "SceneGraphRoot.h" +#include "SGList.h" + +#define FALSE 0 +#define TRUE !FALSE +#define CHAIN_LEN 30 + +static double m = 100.0; +static double k = 1000.0; +static double g = 9.8; +static double dt = 0.01; +static double chain_width = 5; +static double safe = 0.995; + +typedef struct { + double x, y, next_x, next_y; + double vx, vy, next_vx, next_vy; + int can_move; +} CHAIN_VARS; + +CHAIN_VARS cv[CHAIN_LEN]; + +void +init_chain_vars(CHAIN_VARS *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_vector(CHAIN_VARS *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_move_ope(SceneGraphPtr node, int screen_w, int screen_h) +{ + Pad *pad = sgroot->getController(); + + if (pad->circle.isHold()) { + cv[CHAIN_LEN-1].can_move = FALSE; + if (pad->left.isHold()) { + cv[CHAIN_LEN-1].x += -1.0; + } else if (pad->right.isHold()) { + cv[CHAIN_LEN-1].x += 1.0; + } + + if (pad->up.isHold()) { + cv[CHAIN_LEN-1].y += -1.0; + } else if (pad->down.isHold()) { + cv[CHAIN_LEN-1].y += 1.0; + } + } else { + cv[CHAIN_LEN-1].can_move = TRUE; + } +} + +void +chain_move(SceneGraphPtr sg, int w, int h) +{ + int id = sg->id; + if(cv[id].can_move) { + double dx = cv[id-1].x - cv[id].x; + double dy = cv[id-1].y - cv[id].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(id < CHAIN_LEN - 1) { + dx = cv[id+1].x - cv[id].x; + dy = cv[id+1].y - cv[id].y; + l = sqrt(dx * dx + dy * dy); + a = k * (l - chain_width) / m; + ax += a * dx / l; + ay += a * dy / l; + } + ay += g; + cv[id].vx *= safe; + cv[id].vy *= safe; + cv[id].next_vx = cv[id].vx + ax * dt; + cv[id].next_vy = cv[id].vy + ay * dt; + cv[id].next_x = cv[id].x + cv[id].vx * dt; + cv[id].next_y = cv[id].y + cv[id].vy * dt; + } else { + cv[id].next_x = cv[id].x; + cv[id].next_y = cv[id].y; + } + set_vector(&cv[id], sg); + if(id == CHAIN_LEN -1) { + 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; +} + +void +chain_collision(SceneGraphPtr sg, int w, int h, SceneGraphPtr osg) +{ + +} + +void +chain_init(int w, int h) +{ + SceneGraphPtr root_chain, chain; + CHAIN_VARS rcv; + + sgroot->createFromXMLfile("xml_file/chain.xml"); + + root_chain = sgroot->createSceneGraph(CHAIN); + root_chain->set_move_collision(chain_move_ope, chain_collision); + init_chain_vars(&rcv); + rcv.next_x = w / 2; + rcv.next_y = 0.0; + set_vector(&rcv, root_chain); + + for(int i = 0; i < CHAIN_LEN; i++) { + chain = sgroot->createSceneGraph(CHAIN); + chain->id = i; + init_chain_vars(&cv[i]); + if(i == 0) + cv[i].can_move = FALSE; + cv[i].x = 0; + cv[i].y = chain_width * i; + set_vector(&cv[i], chain); + chain->angle[1] = 90 * (i % 2); + chain->set_move_collision(chain_move, chain_collision); + + root_chain->addChild(chain); + } + + sgroot->setSceneData(root_chain); +}