changeset 3:e717e593f213

add walk-example
author e075725
date Wed, 09 Dec 2009 17:29:35 +0900
parents 84443255c672
children 750275385b7a
files programs/walk programs/walk.cc programs/walk.h programs/walk.o
diffstat 4 files changed, 206 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
Binary file programs/walk has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/walk.cc	Wed Dec 09 17:29:35 2009 +0900
@@ -0,0 +1,195 @@
+#include <math.h>
+#include <stdlib.h>
+#include "SceneGraphRoot.h"
+#include "MainLoop.h"
+#include "walk.h"
+
+// prototype
+static void arm_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
+static void ball_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
+static void ball_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree);
+static void ball_collision_idle(SceneGraphPtr, void *sgroot_, int w, int h, SceneGraphPtr tree);
+
+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 v0 = 0.0f; // 初速は 0
+
+static float h0; // 初期高さ
+static float ball_radius = 100.0f;
+
+static float speed = 10.0f;
+
+static void
+ball_move_idle2(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
+{
+    SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
+    Pad *pad = sgroot->getController();
+
+    if (pad->circle.isHold()) {
+        if (pad->left.isHold()) {
+            node->xyz[0] -= speed;
+            if(node->xyz[0] < ball_radius)
+                node->xyz[0] = ball_radius;
+        } else if (pad->right.isHold()) {
+            node->xyz[0] += speed;
+            if(node->xyz[0] > screen_w  - ball_radius)
+                node->xyz[0] = screen_w - ball_radius;
+        }
+
+        if (pad->up.isHold()) {
+            node->xyz[1] -= speed;
+        } else if (pad->down.isHold()) {
+            node->xyz[1] += speed;
+            if(node->xyz[1] > screen_h - ball_radius)
+                node->xyz[1] = screen_h - ball_radius;
+        }
+    } else {
+        node->set_move_collision(ball_move, ball_collision);
+    }
+}
+
+static int time = 0;
+
+static void
+ball_move_idle(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
+{
+    SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
+    Pad *pad = sgroot->getController();
+
+    if (pad->circle.isPush()) {
+        node->set_move_collision(ball_move_idle2, ball_collision_idle);
+        time = 0;
+    }
+
+    time++;
+
+    if (time > 90) {
+        float w = (float)random();
+
+        w = fmodf(w, screen_w - ball_radius*2);
+        node->xyz[0] = w + ball_radius;
+        node->xyz[1] = h0;
+        node->set_move_collision(ball_move, ball_collision);
+        time = 0;
+    }
+}
+
+static void
+ball_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
+{
+    vy += g * dt;
+    node->xyz[1] += vy * dt;
+    //    node->xyz[0] += 10.0f;
+}
+
+static void
+ball_collision_idle(SceneGraphPtr, void *sgroot_, int w, int h, SceneGraphPtr tree)
+{
+}
+
+static void
+ball_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h,
+               SceneGraphPtr tree)
+{
+    if (node->xyz[1] > screen_h - ball_radius) {
+        node->xyz[1] = screen_h - ball_radius;
+
+        vy *= e;
+        if (vy > -g && vy < 0) {
+            vy = 0.0;
+            node->set_move_collision(ball_move_idle, ball_collision_idle);
+        }
+    }
+}
+
+MainLoopPtr
+walk::init(Viewer *sgroot, int screen_w, int screen_h)
+{
+  SceneGraphPtr root, body, head, r_leg_1, l_leg_1, r_leg_2, l_leg_2, r_arm_1, l_arm_1, r_arm_2, l_arm_2;
+
+    // 固定した値で srandom すると、毎回同じ、random() 列が生成される
+    // random な値が欲しいなら、man random に方法が書いてあります。
+    srandom(100);
+
+    sgroot->createFromXMLfile("xml_file/tetsujin_3.xml");
+
+    root = sgroot->createSceneGraph();
+    body = sgroot->createSceneGraph("body");
+    //body->set_move_collision(ball_move, ball_collision);
+
+    head = sgroot->createSceneGraph("head");
+    r_leg_1 = sgroot->createSceneGraph("right_leg_1");
+    l_leg_1 = sgroot->createSceneGraph("left_leg_1");
+    r_leg_2 = sgroot->createSceneGraph("right_leg_2");
+    l_leg_2 = sgroot->createSceneGraph("left_leg_2");
+    r_arm_1 = sgroot->createSceneGraph("right_arm_1");
+    l_arm_1 = sgroot->createSceneGraph("left_arm_1");
+    r_arm_2 = sgroot->createSceneGraph("right_arm_2");
+    l_arm_2 = sgroot->createSceneGraph("left_arm_2");
+
+    root->addChild(body);
+    body->addChild(head);
+    r_leg_2->addChild(r_leg_1);
+    l_leg_2->addChild(l_leg_1);
+    body->addChild(r_leg_2);
+    body->addChild(l_leg_2);
+    r_arm_2->addChild(r_arm_1);
+    l_arm_2->addChild(l_arm_1);
+    body->addChild(r_arm_2);
+    body->addChild(l_arm_2);
+
+    //h0 = screen_h/2;
+    //h0 = -1000;
+
+    body->xyz[0] = screen_w/2;
+    //ball->xyz[0] = 0.0f;
+    body->xyz[1] = screen_h/2 + 25;
+    body->xyz[2] = 0.0f;
+    body->angle[0] = -90.0f;
+
+    r_arm_1->angle[1] = -45.0f;
+    r_arm_1->xyz[0] = 0;
+    r_arm_1->xyz[1] = 0;
+    r_arm_1->xyz[2] = 0;
+
+    sgroot->setSceneData(root);
+
+    return sgroot;
+}
+
+MainLoopPtr
+walk::init_only_sg(SgChange *sgroot, int screen_w, int screen_h)
+{
+    return sgroot;
+}
+
+extern Application *
+application() {
+  return new walk();
+}
+
+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 */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/programs/walk.h	Wed Dec 09 17:29:35 2009 +0900
@@ -0,0 +1,11 @@
+#include <math.h>
+#include <stdlib.h>
+#include "SceneGraphRoot.h"
+#include "Application.h"
+#include "MainLoop.h"
+
+class walk : public Application {
+
+    MainLoopPtr init(Viewer *viewer, int screen_w, int screen_h);
+    MainLoopPtr init_only_sg(SgChange *viewer, int screen_w, int screen_h);
+};
Binary file programs/walk.o has changed