Class: GameMachine::Navigation::DetourPath

Inherits:
Object
  • Object
show all
Defined in:
server/lib/game_machine/navigation/detour_path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(navmesh) ⇒ DetourPath


14
15
16
17
18
19
20
21
22
23
# File 'server/lib/game_machine/navigation/detour_path.rb', line 14

def initialize(navmesh)
  @navmesh = navmesh
  @max_paths = 256
  @error = nil

  unless navmesh.loaded?
    raise "Navmesh #{navmesh.id} not loaded"
  end
  @query_ptr = Detour.getQuery(navmesh.id)
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error


13
14
15
# File 'server/lib/game_machine/navigation/detour_path.rb', line 13

def error
  @error
end

#max_pathsObject (readonly)

Returns the value of attribute max_paths


13
14
15
# File 'server/lib/game_machine/navigation/detour_path.rb', line 13

def max_paths
  @max_paths
end

Returns the value of attribute navmesh


13
14
15
# File 'server/lib/game_machine/navigation/detour_path.rb', line 13

def navmesh
  @navmesh
end

Class Method Details

.query_ref(navmesh_id) ⇒ Object


5
6
7
8
9
10
11
# File 'server/lib/game_machine/navigation/detour_path.rb', line 5

def self.query_ref(navmesh_id)
  if navmesh = DetourNavmesh.find(navmesh_id)
    Thread.current[navmesh_id.to_s.to_sym] ||= new(navmesh)
  else
    raise "Navmesh with id #{navmesh_id} not found"
  end
end

Instance Method Details

#destroy_queryObject


25
26
27
# File 'server/lib/game_machine/navigation/detour_path.rb', line 25

def destroy_query
  Detour.freeQuery(@query_ptr)
end

#find_path(start_x, start_y, start_z, end_x, end_y, end_z, straight_path) ⇒ Object

Detour coords: z = unity x, x = unity z


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'server/lib/game_machine/navigation/detour_path.rb', line 31

def find_path( start_x, start_y, start_z, end_x, end_y, end_z,straight_path)

  @error = nil

  ptr = Detour.getPathPtr(max_paths)
  paths_found = Detour.findPath(
    @query_ptr,start_z,start_y,
    start_x, end_z,end_y,end_x, straight_path, ptr
  )

  if paths_found <= 0
    @error = paths_found
    return []
  end

  fptr = ptr.read_pointer()
  path = fptr.null? ? [] : ptr.get_array_of_float(0,paths_found*3)
  Detour.freePath(ptr)
  path.each_slice(3).map {|i| Vector.new(i[2],i[1],i[0])}.to_a
end