aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/surface_sphere.go (8997B)


      1 package main
      2 
      3 import "math"
      4 
      5 type surfaceTriangle struct {
      6 	normal        [3]float32
      7 	edges, sides  [3][3]float32
      8 	lengths       [3]float32
      9 	plane         float32
     10 	side, tangent [3]float32
     11 }
     12 
     13 type surfaceContact struct {
     14 	point, normal [3]float32
     15 	distance      float32
     16 	hit           bool
     17 }
     18 
     19 func surfaceDot(first, second [3]float32) float64 {
     20 	return float64(first[0])*float64(second[0]) + float64(first[1])*float64(second[1]) + float64(first[2])*float64(second[2])
     21 }
     22 
     23 func surfaceCross(first, second [3]float32) [3]float32 {
     24 	return [3]float32{
     25 		float32(float64(first[1])*float64(second[2]) - float64(first[2])*float64(second[1])),
     26 		float32(float64(first[2])*float64(second[0]) - float64(first[0])*float64(second[2])),
     27 		float32(float64(first[0])*float64(second[1]) - float64(first[1])*float64(second[0])),
     28 	}
     29 }
     30 
     31 func surfaceNormal(first, second [3]float32) [3]float32 {
     32 	normal := surfaceCross(first, second)
     33 	length := math.Sqrt(surfaceDot(normal, normal))
     34 	if length == 0 {
     35 		return [3]float32{0, 1, 0}
     36 	}
     37 	for axis := range normal {
     38 		normal[axis] = float32(float64(normal[axis]) / length)
     39 	}
     40 	return normal
     41 }
     42 
     43 func newSurfaceTriangle(vertices [3][3]float32) surfaceTriangle {
     44 	// Source: client/game/llr.dll/lifted-0028.c, sub_380468C0.
     45 	// Derive oriented plane coefficients from ordered unit edges.
     46 	triangle := surfaceTriangle{}
     47 	for index := range 3 {
     48 		for axis := range 3 {
     49 			triangle.edges[index][axis] = vertices[(index+1)%3][axis] - vertices[index][axis]
     50 		}
     51 		triangle.lengths[index] = float32(math.Sqrt(surfaceDot(triangle.edges[index], triangle.edges[index])))
     52 		if triangle.lengths[index] != 0 {
     53 			for axis := range 3 {
     54 				triangle.edges[index][axis] /= triangle.lengths[index]
     55 			}
     56 		}
     57 	}
     58 	triangle.normal = surfaceNormal(triangle.edges[2], triangle.edges[0])
     59 	triangle.plane = float32(-surfaceDot(triangle.normal, vertices[0]))
     60 	for index := range 3 {
     61 		triangle.sides[index] = surfaceNormal(triangle.edges[index], triangle.normal)
     62 		triangle.side[index] = float32(-surfaceDot(triangle.sides[index], vertices[index]))
     63 		triangle.tangent[index] = float32(-surfaceDot(triangle.edges[index], vertices[index]))
     64 	}
     65 	return triangle
     66 }
     67 
     68 func (triangle *surfaceTriangle) candidate(center [3]float32, limit float32) (float32, float32, bool) {
     69 	// client/game/llr.dll/lifted-0028.c, sub_38046C00: first positive edge plane.
     70 	distance := surfaceDot(triangle.normal, center) + float64(triangle.plane)
     71 	squared := float32(distance * distance)
     72 	gate := float64(limit + float32(.0001))
     73 	if distance < 0 || float64(squared) > gate {
     74 		return 0, 0, false
     75 	}
     76 	for index := range 3 {
     77 		side := surfaceDot(triangle.sides[index], center) + float64(triangle.side[index])
     78 		if side <= 0 {
     79 			continue
     80 		}
     81 		total := float64(squared) + side*side
     82 		if total >= gate {
     83 			return 0, 0, false
     84 		}
     85 		tangent := surfaceDot(triangle.edges[index], center) + float64(triangle.tangent[index])
     86 		if tangent < 0 {
     87 			total += tangent * tangent
     88 		} else if tangent > float64(triangle.lengths[index]) {
     89 			extra := tangent - float64(triangle.lengths[index])
     90 			total += extra * extra
     91 		}
     92 		if total >= gate {
     93 			return 0, 0, false
     94 		}
     95 		return float32(total), float32(total - float64(squared)), true
     96 	}
     97 	return squared, 0, true
     98 }
     99 
    100 func (triangle *surfaceTriangle) contact(center [3]float32) surfaceContact {
    101 	// Source: client/game/llr.dll/lifted-0028.c, sub_38046D80.
    102 	distance := float32(surfaceDot(triangle.normal, center) + float64(triangle.plane))
    103 	if distance < 0 {
    104 		return surfaceContact{}
    105 	}
    106 	contact := surfaceContact{normal: triangle.normal, distance: distance, hit: true}
    107 	for axis := range 3 {
    108 		contact.point[axis] = center[axis] - distance*triangle.normal[axis]
    109 	}
    110 	for index := range 3 {
    111 		side := float32(surfaceDot(triangle.sides[index], center) + float64(triangle.side[index]))
    112 		if side <= 0 {
    113 			continue
    114 		}
    115 		tangent := float32(surfaceDot(triangle.edges[index], center) + float64(triangle.tangent[index]))
    116 		extra := float32(0)
    117 		if tangent < 0 {
    118 			extra = tangent
    119 		} else if tangent > triangle.lengths[index] {
    120 			extra = tangent - triangle.lengths[index]
    121 		}
    122 		for axis := range 3 {
    123 			contact.point[axis] -= side * triangle.sides[index][axis]
    124 			contact.point[axis] -= extra * triangle.edges[index][axis]
    125 		}
    126 		squared := float32(float64(distance)*float64(distance) + float64(side)*float64(side) + float64(extra)*float64(extra))
    127 		contact.distance = float32(math.Sqrt(float64(squared)))
    128 		break
    129 	}
    130 	return contact
    131 }
    132 
    133 func (group *surfaceGroup) sphere(center [3]float32, bound float32) surfaceContact {
    134 	// Source: client/game/llr.dll/lifted-0028.c, sub_38047510.
    135 	best, lateral := bound*bound, float32(999999995904)
    136 	selected := -1
    137 	for index := range group.geometry {
    138 		squared, side, hit := group.geometry[index].candidate(center, best)
    139 		if hit && (float64(squared)+float64(float32(.0001)) < float64(best) || side < lateral) {
    140 			best, lateral, selected = squared, side, index
    141 		}
    142 	}
    143 	if selected == -1 {
    144 		return surfaceContact{}
    145 	}
    146 	return group.geometry[selected].contact(center)
    147 }
    148 
    149 func surfaceOverlap(bounds [6]float32, center [3]float32, radius float32) bool {
    150 	for axis := range 3 {
    151 		if float64(center[axis])+float64(radius) < float64(bounds[axis]) || float64(center[axis])-float64(radius) > float64(bounds[axis+3]) {
    152 			return false
    153 		}
    154 	}
    155 	return true
    156 }
    157 
    158 func (tree *surfaceTree) initialize() {
    159 	// Lazy coefficients are immutable after construction, including authored bounds.
    160 	tree.prepare.Do(func() {
    161 		for index := range tree.groups {
    162 			group := &tree.groups[index]
    163 			group.geometry = make([]surfaceTriangle, len(group.triangles))
    164 			for index, vertices := range group.triangles {
    165 				group.geometry[index] = newSurfaceTriangle([3][3]float32{tree.vertices[vertices[0]], tree.vertices[vertices[1]], tree.vertices[vertices[2]]})
    166 			}
    167 		}
    168 		for index := len(tree.nodes) - 1; index >= 0; index-- {
    169 			node := &tree.nodes[index]
    170 			bounds := make([][6]float32, 0, len(node.groups)+2)
    171 			for _, group := range node.groups {
    172 				bounds = append(bounds, tree.groups[group].bounds)
    173 			}
    174 			if len(node.groups) == 0 {
    175 				for _, child := range node.children {
    176 					bounds = append(bounds, tree.nodes[child].bounds)
    177 				}
    178 			}
    179 			node.bounds = bounds[0]
    180 			for _, bound := range bounds[1:] {
    181 				for axis := range 3 {
    182 					node.bounds[axis] = min(node.bounds[axis], bound[axis])
    183 					node.bounds[axis+3] = max(node.bounds[axis+3], bound[axis+3])
    184 				}
    185 			}
    186 		}
    187 	})
    188 }
    189 
    190 func (surface *sceneSurface) sphere(center [3]float32, radius float32) surfaceContact {
    191 	if surface.primary == nil {
    192 		return surfaceContact{}
    193 	}
    194 	// Resource sphere checks include invisible geometry; the camera-only scope is separate.
    195 	// Source: client/game/llr.dll/lifted-0021.c, sub_38037D20.
    196 	invisible := surfaceContact{}
    197 	if surface.invisible != nil {
    198 		invisible = surface.invisible.sphere(center, radius)
    199 	}
    200 	primary := surface.primary.sphere(center, radius)
    201 	if !invisible.hit || primary.hit && surfaceDistanceSquared(center, primary.point) < surfaceDistanceSquared(center, invisible.point) {
    202 		return primary
    203 	}
    204 	return invisible
    205 }
    206 
    207 func surfaceDistanceSquared(first, second [3]float32) float64 {
    208 	difference := [3]float32{first[0] - second[0], first[1] - second[1], first[2] - second[2]}
    209 	return surfaceDot(difference, difference)
    210 }
    211 
    212 func (tree *surfaceTree) sphere(center [3]float32, radius float32) surfaceContact {
    213 	tree.initialize()
    214 	return tree.nodeSphere(0, center, radius, radius)
    215 }
    216 
    217 func (tree *surfaceTree) nodeSphere(index int, center [3]float32, radius, bound float32) surfaceContact {
    218 	// Source: client/game/llr.dll/lifted-0027.c, sub_380447B0.
    219 	// Keep the first-subtree result while traversing its tail.
    220 	found := surfaceContact{}
    221 	node := &tree.nodes[index]
    222 	for len(node.groups) == 0 {
    223 		if !surfaceOverlap(node.bounds, center, bound) {
    224 			return found
    225 		}
    226 		position, split := float64(center[node.axis]), float64(node.split)
    227 		if !(position < split+float64(bound)) {
    228 			node = &tree.nodes[node.children[1]]
    229 			continue
    230 		}
    231 		if !(position >= split-float64(bound)) {
    232 			node = &tree.nodes[node.children[0]]
    233 			continue
    234 		}
    235 		first, next := node.children[0], node.children[1]
    236 		if position >= split {
    237 			first, next = next, first
    238 		}
    239 		contact := tree.nodeSphere(first, center, radius, bound)
    240 		if contact.hit && contact.distance <= bound {
    241 			if position >= split && split+float64(contact.distance) <= position || position < split && split-float64(contact.distance) > position {
    242 				return contact
    243 			}
    244 			bound, found = contact.distance, contact
    245 		}
    246 		node = &tree.nodes[next]
    247 	}
    248 	if len(node.groups) > 1 && !surfaceOverlap(node.bounds, center, radius) {
    249 		return found
    250 	}
    251 	if !found.hit && bound == radius {
    252 		bound += .01
    253 	}
    254 	for _, index := range node.groups {
    255 		group := &tree.groups[index]
    256 		if !surfaceOverlap(group.bounds, center, bound) {
    257 			continue
    258 		}
    259 		contact := group.sphere(center, bound)
    260 		if contact.hit && contact.distance >= 0 && contact.distance <= radius && contact.distance < bound {
    261 			bound, found = contact.distance, contact
    262 		}
    263 	}
    264 	return found
    265 }