changeset 17:bf87fe5a9797

ver3
author <e085737>
date Fri, 10 Dec 2010 20:19:03 +0900
parents 9367d87879ee
children 931ad40d56f5
files Pants_FPS/PantsFPS.html Pants_FPS/resources/SceneGraph.js Pants_FPS/resources/SceneGraphRoot.js Pants_FPS/resources/human.js Pants_FPS/resources/walk.js
diffstat 5 files changed, 83 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/Pants_FPS/PantsFPS.html	Fri Dec 10 16:42:24 2010 +0900
+++ b/Pants_FPS/PantsFPS.html	Fri Dec 10 20:19:03 2010 +0900
@@ -77,7 +77,7 @@
         width = 1;
 	height = -1;
         
-        function reshape(ctx)
+        function reshape(ctx, sgroot)
 	{
 	    var canvas = document.getElementById('example');
             if (canvas.width == width && canvas.width == height)
@@ -88,16 +88,8 @@
 	    // 画面の調節はいまだ手作業
 
             ctx.viewport(0, 0, width, height);
-	    ctx.perspectiveMatrix = new J3DIMatrix4();
-
-	    ctx.perspectiveMatrix.perspective(45, width/height, 1, 10000);
-	    ctx.perspectiveMatrix.rotate(cameraAngle_xyz[0],0,1,0)
-	    ctx.perspectiveMatrix.rotate(cameraAngle_xyz[1], angleRate_xyz[0], 0, angleRate_xyz[2])
-	    //ctx.perspectiveMatrix.rotate(cameraAngle_xyz[1], -1, 0, 1)
-
-
-            ctx.perspectiveMatrix.lookat(position_xyz[0],position_xyz[1],position_xyz[2], aim_xyz[0], aim_xyz[1], aim_xyz[2], 0, -1, 0);
-            // ctx.perspectiveMatrix.lookat(0, 0, -500, 0, 0, 0, 0, -1, 0);
+	    //ctx.perspectiveMatrix = new J3DIMatrix4()
+	    //sgroot.getCamera(ctx)
 
         }
         
@@ -152,8 +144,9 @@
         
         function drawPicture(ctx,sgroot,w,h)
         {
-            reshape(ctx);
-            ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
+            reshape(ctx, sgroot)
+            ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT)
+	    //sgroot.getCamera(ctx)
 
             MainLoop(ctx,sgroot,w,h)
 
--- a/Pants_FPS/resources/SceneGraph.js	Fri Dec 10 16:42:24 2010 +0900
+++ b/Pants_FPS/resources/SceneGraph.js	Fri Dec 10 20:19:03 2010 +0900
@@ -3,6 +3,7 @@
 	collision_object(sg_exec_tree,sgroot,w,h)
         move_object(sg_exec_tree,sgroot,w,h)
 	draw_object(ctx,sg_exec_tree,sgroot)
+	camera_object(ctx, sgroot, sgroot.camera)
 }
 
 
@@ -47,6 +48,11 @@
 }
 
 
+function camera_object(ctx, sgroot, node){
+    sgroot.getCamera(ctx, node)
+}
+
+
 hand_mat = function(node){
     for(var num in node.child){
 	    node.child[num].parents_mat = new J3DIMatrix4()
--- a/Pants_FPS/resources/SceneGraphRoot.js	Fri Dec 10 16:42:24 2010 +0900
+++ b/Pants_FPS/resources/SceneGraphRoot.js	Fri Dec 10 20:19:03 2010 +0900
@@ -13,6 +13,7 @@
     aim_xyz = new Array(0 ,0, 1)
     eye_xyz = new Array(0, -1, 0)
     this.sg_src = new Array()
+    this.camera = null
 
     angleRate_xyz = new Array(1, 0, 0)
 
@@ -52,22 +53,6 @@
 }
 
 
-SceneGraphRoot.prototype.createSceneGraph2 = function(name){
-    if(name < 0){
-	alert("error: createSceneGraph(name): name object not found.")
-        return null
-    }
-
-    for(num in sg_src){
-	if(name == sg_src[num].name){
-	    var src = new SceneGraph(name)
-	}
-    }
-
-    return src
-}
-
-
 SceneGraphRoot.prototype.createSceneGraph3 = function(){
    var p = new SceneGraph() 
 
@@ -96,6 +81,34 @@
 }
 
 
+SceneGraphRoot.prototype.setCamera = function(sgroot, node){
+    this.camera = node
+}
+
+
+
+SceneGraphRoot.prototype.getCamera = function(ctx, node){
+    if(node.mat != null){
+        ctx.perspectiveMatrix = new J3DIMatrix4()
+        node.mat.getAsArrayMatrix(ctx.perspectiveMatrix.$matrix)
+    }else{
+	ctx.perspectiveMatrix = new J3DIMatrix4()
+    }
+
+    ctx.perspectiveMatrix.perspective(45, width/height, 1, 10000);
+    ctx.perspectiveMatrix.rotate(cameraAngle_xyz[0],0,1,0)
+    ctx.perspectiveMatrix.rotate(cameraAngle_xyz[1], angleRate_xyz[0], 0, angleRate_xyz[2])
+    
+
+    ctx.perspectiveMatrix.lookat(position_xyz[0],position_xyz[1],position_xyz[2], aim_xyz[0], aim_xyz[1], aim_xyz[2], 0, -1, 0);
+    //ctx.perspectiveMatrix.lookat( 0, 0, 0, 0, 0, 1, 0, -1, 0);
+}
+
+
+
+
+
+
 SceneGraphRoot.prototype.getController = function(){   
     var left_push     = currentlyPressedKeys[37]     /* ←  */
     var down_push     = currentlyPressedKeys[40]     /* ↓  */
--- a/Pants_FPS/resources/human.js	Fri Dec 10 16:42:24 2010 +0900
+++ b/Pants_FPS/resources/human.js	Fri Dec 10 20:19:03 2010 +0900
@@ -80,6 +80,7 @@
     var move_eye = 1.0 
     var PI = 3.14
     var pad = new sgroot.getController()
+    var d = 30
 
     if(pad.left_isHold()){
 	cameraAngle_xyz[0] -= move_eye
@@ -88,7 +89,13 @@
 	var radian = ((theta%360) / 360) * 2 * PI
 	var x_rate = Math.sin(radian)
 	var z_rate = Math.cos(radian)
-
+        var mv_x = d * Math.cos(radian)
+	var mv_z = d * Math.sin(radian)
+         
+	position_xyz[0] = node.parents.xyz[0] - mv_x
+	position_xyz[2] = node.parents.xyz[2] - mv_z	
+	aim_xyz[0] = position_xyz[0] 
+	aim_xyz[2] = position_xyz[2] + 1
 	node.angle[0] -= move_eye 
 	angleRate_xyz[0] = x_rate
 	angleRate_xyz[2] = z_rate
@@ -102,7 +109,13 @@
 	var radian = ((theta%360) / 360) * 2 * PI
 	var x_rate = Math.sin(radian)
 	var z_rate = Math.cos(radian)
+	var mv_x = d * Math.cos(radian)
+	var mv_z = d * Math.sin(radian)
 
+	position_xyz[0] = node.parents.xyz[0] - mv_x
+	position_xyz[2] = node.parents.xyz[2] - mv_z
+	aim_xyz[0] = position_xyz[0]
+	aim_xyz[2] = position_xyz[2] + 1
 	node.angle[0] += move_eye
 	angleRate_xyz[0] = x_rate
 	angleRate_xyz[2] = z_rate
@@ -133,7 +146,7 @@
     var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dz,2))
 
 
-    if(position_xyz[0] >= 0 && position_xyz[2] >= 0){
+    if(node.xyz[0] >= 0 && node.xyz[2] >= 0){
         if(pad.left_move_isHold()){
   	    var radian = (((theta+90)%360) / 360) * 2 * PI
           
@@ -224,14 +237,33 @@
 
 
 body_collision = function(node, sgroot, w, h){
-    if(position_xyz[0] < 0){
-        position_xyz[0] = 0
-	aim_xyz[0] = 0
+    var d = 30
+    var PI = 3.14
+    var radian = ((theta%360) / 360) * 2 * PI
+    if(node.xyz[0] < 0){
+        node.xyz[0] = 0
+	position_xyz[0] = -d * Math.cos(radian)
+	aim_xyz[0] = position_xyz[0]
     }
 
-    if(position_xyz[2] < 0){
-	position_xyz[2] = 0
-	aim_xyz[2] = 1
+    if(node.xyz[0] > 350){
+	node.xyz[0] = 350
+        position_xyz[0] = 350 + (-d * Math.cos(radian))
+	aim_xyz[0] = position_xyz[0]
+    }
+
+
+
+    if(node.xyz[2] < 0){
+	node.xyz[2] = 0
+	position_xyz[2] = -d * Math.sin(radian)
+	aim_xyz[2] = position_xyz[2] + 1
+    }
+
+    if(node.xyz[2] > 350){
+	node.xyz[2] = 350
+	position_xyz[2] = 350 + (-d * Math.sin(radian))
+	aim_xyz[2] = position_xyz[2] + 1
     }
 }
 
--- a/Pants_FPS/resources/walk.js	Fri Dec 10 16:42:24 2010 +0900
+++ b/Pants_FPS/resources/walk.js	Fri Dec 10 20:19:03 2010 +0900
@@ -78,8 +78,9 @@
     root.addChild(land)
     root.addChild(walk_man)
     //land.addChild(wall)
-    //root.addChild(pants)
+    root.addChild(pants)
 
+    sgroot.setCamera(sgroot, root)
     sgroot.setSceneData(root)
 
     //return sgroot