Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added in maze. floor and four walls

  • Loading branch information...
commit c224e628e07890ad77a79c90f73411b010df773d 1 parent afcabc3
Jeremy Woertink authored October 07, 2011
BIN  assets/Textures/brickwall.jpg
BIN  assets/Textures/hardwood.jpg
148  lib/maze.rb
... ...
@@ -0,0 +1,148 @@
  1
+require 'java'
  2
+PROJECT_ROOT = File.expand_path('..', File.dirname(__FILE__))
  3
+require File.join(PROJECT_ROOT, 'vendor', 'jme3_2011-08-29.jar')
  4
+
  5
+java_import "com.jme3.app.SimpleApplication"
  6
+java_import "com.jme3.bullet.BulletAppState"
  7
+java_import "com.jme3.bullet.collision.shapes.CapsuleCollisionShape"
  8
+java_import "com.jme3.bullet.collision.shapes.CollisionShape"
  9
+java_import "com.jme3.bullet.control.CharacterControl"
  10
+java_import "com.jme3.bullet.control.RigidBodyControl"
  11
+java_import "com.jme3.bullet.util.CollisionShapeFactory"
  12
+java_import "com.jme3.input.KeyInput"
  13
+java_import "com.jme3.input.controls.ActionListener"
  14
+java_import "com.jme3.input.controls.KeyTrigger"
  15
+java_import "com.jme3.light.AmbientLight"
  16
+java_import "com.jme3.light.DirectionalLight"
  17
+java_import "com.jme3.math.ColorRGBA"
  18
+java_import "com.jme3.math.Vector3f"
  19
+java_import "com.jme3.scene.Node"
  20
+java_import "com.jme3.scene.Spatial"
  21
+java_import "com.jme3.scene.shape.Box"
  22
+java_import "com.jme3.scene.Geometry"
  23
+java_import "com.jme3.material.Material"
  24
+
  25
+class Maze < SimpleApplication
  26
+  include ActionListener
  27
+  
  28
+  field_accessor :flyCam
  29
+  field_reader :cam
  30
+  attr_accessor :bullet_app_state, :player
  31
+  
  32
+  def initialize
  33
+    [:up, :down, :left, :right].each { |direction| self.instance_variable_set("@#{direction}", false) }
  34
+    @walk_direction = Vector3f.new
  35
+  end
  36
+  
  37
+  def simpleInitApp
  38
+    self.bullet_app_state = BulletAppState.new
  39
+    state_manager.attach(bullet_app_state)
  40
+    view_port.background_color = ColorRGBA.new(ColorRGBA.random_color)
  41
+    
  42
+    
  43
+    capsule_shape = CapsuleCollisionShape.new(1.5, 6.0, 1)
  44
+    self.player = CharacterControl.new(capsule_shape, 0.05)
  45
+    player.jump_speed = 20
  46
+    player.fall_speed = 30
  47
+    player.gravity = 30
  48
+    player.physics_location = Vector3f.new(0, 10, 0)
  49
+    bullet_app_state.physics_space.add(player)
  50
+    bullet_app_state.physics_space.enable_debug(asset_manager)
  51
+    
  52
+    setup_camera!
  53
+    setup_floor!
  54
+    create_wall(0, 0, 100, 100, 10, 0)
  55
+    create_wall(0, 0, -100, 100, 10, 0)
  56
+    create_wall(100, 0, 0, 0, 10, 100)
  57
+    create_wall(-100, 0, 0, 0, 10, 100)
  58
+    
  59
+    setup_keys!
  60
+    setup_light!
  61
+  end
  62
+  
  63
+  def setup_camera!
  64
+    flyCam.move_speed = 100
  65
+  end
  66
+  
  67
+  def setup_floor!
  68
+    box = Box.new(Vector3f.new(0, 0, 0), 100, 0.2, 100)
  69
+    floor = Geometry.new("the Floor", box)
  70
+    matl = Material.new(asset_manager, File.join("Common", "MatDefs", "Misc", "Unshaded.j3md"))
  71
+    matl.set_texture("ColorMap", asset_manager.load_texture(File.join('assets', 'Textures', 'hardwood.jpg')))
  72
+    floor.material = matl    
  73
+    scene_shape = CollisionShapeFactory.create_mesh_shape(floor)
  74
+    landscape = RigidBodyControl.new(scene_shape, 0)
  75
+    floor.add_control(landscape)
  76
+    bullet_app_state.physics_space.add(landscape)
  77
+    root_node.attach_child(floor)
  78
+  end
  79
+  
  80
+  #  vx = x position
  81
+  #  vy = elevation
  82
+  #  vz = y position
  83
+  #  bx = x width
  84
+  #  by = thickness (height)
  85
+  #  bz = y width
  86
+  def create_wall(vx, vy, vz, bx, by, bz)
  87
+    box = Box.new(Vector3f.new(vx, vy, vz), bx, by, bz)
  88
+    wall = Geometry.new("a Wall", box)
  89
+    matl = Material.new(asset_manager, File.join("Common", "MatDefs", "Misc", "Unshaded.j3md"))
  90
+    #matl.set_color("Color", ColorRGBA::Gray)
  91
+    matl.set_texture("ColorMap", asset_manager.load_texture(File.join('assets', 'Textures', 'brickwall.jpg')))
  92
+    wall.material = matl
  93
+    scene_shape = CollisionShapeFactory.create_mesh_shape(wall)
  94
+    landscape = RigidBodyControl.new(scene_shape, 0)
  95
+    wall.add_control(landscape)
  96
+    bullet_app_state.physics_space.add(landscape)
  97
+    root_node.attach_child(wall)
  98
+  end
  99
+  
  100
+  def setup_light!
  101
+    al = AmbientLight.new
  102
+    al.color = ColorRGBA::White.mult(1.3)
  103
+    root_node.add_light(al)
  104
+    dl = DirectionalLight.new
  105
+    dl.color = ColorRGBA::White
  106
+    dl.direction = Vector3f.new(2.8, -2.8, -2.8).normalize_local
  107
+    root_node.add_light(dl)
  108
+  end
  109
+  
  110
+  def setup_keys!
  111
+    input_manager.add_mapping("Left",  KeyTrigger.new(KeyInput::KEY_A))
  112
+    input_manager.add_mapping("Right", KeyTrigger.new(KeyInput::KEY_D))
  113
+    input_manager.add_mapping("Up",    KeyTrigger.new(KeyInput::KEY_W))
  114
+    input_manager.add_mapping("Down",  KeyTrigger.new(KeyInput::KEY_S))
  115
+    input_manager.add_listener(ControllerAction.new(self), "Left")
  116
+    input_manager.add_listener(ControllerAction.new(self), "Right")
  117
+    input_manager.add_listener(ControllerAction.new(self), "Up")
  118
+    input_manager.add_listener(ControllerAction.new(self), "Down")
  119
+  end
  120
+  
  121
+  def simpleUpdate(tpf)
  122
+    cam_dir = cam.direction.clone.mult_local(0.6)
  123
+    cam_left = cam.left.clone.mult_local(0.4)
  124
+    @walk_direction.set(0, 0, 0)
  125
+    @walk_direction.add_local(cam_left) if @left
  126
+    @walk_direction.add_local(cam_left.negate) if @right
  127
+    @walk_direction.add_local(cam_dir) if @up
  128
+    @walk_direction.add_local(cam_dir.negate) if @down
  129
+    player.walk_direction = @walk_direction
  130
+    cam.location = player.physics_location
  131
+  end
  132
+  
  133
+  class ControllerAction
  134
+    include ActionListener
  135
+    
  136
+    def initialize(obj)
  137
+      @parent = obj
  138
+    end
  139
+    
  140
+    def on_action(binding, value, tpf)
  141
+      @parent.instance_variable_set("@#{binding.downcase}", value)
  142
+    end
  143
+    
  144
+  end
  145
+  
  146
+end
  147
+
  148
+Maze.new.start
2  lib/sample8.rb
@@ -74,7 +74,7 @@ def setup_light!
74 74
     al.color = ColorRGBA::White.mult(1.3)
75 75
     root_node.add_light(al)
76 76
     dl = DirectionalLight.new
77  
-    dl.color = ColorRGBA::White
  77
+    dl.color = ColorRGBA.new(0.75, 1.0, 0.0, 1.0)
78 78
     dl.direction = Vector3f.new(2.8, -2.8, -2.8).normalize_local
79 79
     root_node.add_light(dl)
80 80
   end
47  lib/sample9.rb
@@ -11,42 +11,45 @@
11 11
 
12 12
 class Sample9 < SimpleApplication
13 13
   field_accessor :flyCam
14  
-  attr_accessor :terrain, :mat_terrain
15  
-  
16 14
   def simpleInitApp
17 15
     begin
18 16
       flyCam.move_speed = 50
19 17
     
20  
-      self.mat_terrain = Material.new(asset_manager, File.join("Common", "MatDefs", "Terrain", "Terrain.j3md"))
21  
-      mat_terrain.set_texture("Alpha", asset_manager.load_texture(File.join("Textures", "Terrain", "splat", "alphamap.png")))
22  
-    
  18
+      @mat_terrain = Material.new(asset_manager, File.join("Common", "MatDefs", "Terrain", "Terrain.j3md"))
  19
+      @mat_terrain.set_texture("Alpha", asset_manager.load_texture(File.join("Textures", "Terrain", "splat", "alphamap.png")))
  20
+
23 21
       grass = asset_manager.load_texture(File.join("Textures", "Terrain", "splat", "grass.jpg"))
24 22
       grass.wrap = Texture::WrapMode::Repeat
25  
-      mat_terrain.set_texture("Tex1", grass)
26  
-      mat_terrain.set_float("Tex1Scale", 64.0)
27  
-    
  23
+      @mat_terrain.set_texture("Tex1", grass)
  24
+      @mat_terrain.set_float("Tex1Scale", 64.0)
  25
+      
28 26
       dirt = asset_manager.load_texture(File.join("Textures", "Terrain", "splat", "dirt.jpg"))
29 27
       dirt.wrap = Texture::WrapMode::Repeat
30  
-      mat_terrain.set_texture("Tex2", dirt)
31  
-      mat_terrain.set_float("Tex2Scale", 32.0)
32  
-    
  28
+      @mat_terrain.set_texture("Tex2", dirt)
  29
+      @mat_terrain.set_float("Tex2Scale", 32.0)
  30
+
33 31
       rock = asset_manager.load_texture(File.join("Textures", "Terrain", "splat", "road.jpg"))
34 32
       rock.wrap = Texture::WrapMode::Repeat
35  
-      mat_terrain.set_texture("Tex3", rock)
36  
-      mat_terrain.set_float("Tex3Scale", 128.0)
  33
+      @mat_terrain.set_texture("Tex3", rock)
  34
+      @mat_terrain.set_float("Tex3Scale", 128.0)
  35
+
37 36
       height_map_image = asset_manager.load_texture(File.join("Textures", "Terrain", "splat", "mountains512.png"))
38 37
       heightmap = ImageBasedHeightMap.new(ImageToAwt.convert(height_map_image.image, false, true, 0))
39 38
       heightmap.load
40  
-    
  39
+      
41 40
       patch_size = 65
42  
-      self.terrain = TerrainQuad.new("my terrain", patch_size, 513, heightmap.height_map)
43  
-      terrain.material = mat_terrain
44  
-      terrain.set_local_translation(0, -100, 0)
45  
-      terrain.set_local_scale(2.0, 1.0, 2.0)
46  
-      root_node.attach_child(terrain)
47  
-      control = TerrainLodControl.new(terrain, get_camera)
48  
-      control.lod_calculator = DistanceLodCalculator.new(patch_size, 2.7)
49  
-      terrain.add_control(control)
  41
+      @terrain = TerrainQuad.new("my terrain", patch_size, 513, heightmap.height_map)
  42
+      @terrain.material = @mat_terrain
  43
+      @terrain.set_local_translation(0, -100, 0)
  44
+      @terrain.set_local_scale(2.0, 1.0, 2.0)
  45
+      
  46
+      root_node.attach_child(@terrain)
  47
+      control = TerrainLodControl.new(@terrain, get_camera)
  48
+      
  49
+      # FAILS HERE....
  50
+      #control.lod_calculator = DistanceLodCalculator.new(patch_size, 2.7)
  51
+      @terrain.add_control(control)
  52
+      puts "finish...."
50 53
     rescue => e
51 54
       puts "#{e}"
52 55
     end

0 notes on commit c224e62

Please sign in to comment.
Something went wrong with that request. Please try again.