Module MyGameUtils
[hide private]
[frames] | no frames]

Source Code for Module MyGameUtils

  1  from math import cos, sin, pi, ceil, fabs 
  2  from Mathutils import RotationMatrix 
  3  from MyScene import MY_SingleScene 
  4   
5 -def GetBoxCollision(obj1, obj2):
6 """ 7 calculates the collsion of game objects, like patforms and stairs 8 9 @rtype: Boolean 10 @return: 'False' if there is a coillision, else 'True' 11 """ 12 13 intersection = False 14 15 return intersection
16
17 -def SetReverseRootConnection(root_ground):
18 """ 19 sets the right id as the root connected id 20 21 @type root_ground: MY_GameObject 22 @param root_ground: the object that represents the current ground object of the player 23 """ 24 25 for idx, child_object in root_ground.connectedSides.iteritems(): 26 if child_object != None: 27 if child_object.rootConnectObject == None: 28 child_object.rootConnectObject = root_ground 29 30 if child_object.rootConnectObject != root_ground: 31 child_object.rootConnectObject = root_ground 32 SetReverseRootConnection(child_object)
33
34 -def GetGlobalOrientationToMe(source_object, target_object):
35 """ 36 gets the global direction of an object to me (at top, bottom, left, right, front or back) 37 question is: where is an object from my point of view (globally)? 38 39 @type source_object: MY_GameObject 40 @param source_object: the source object to whom you want the orientation to (dont know how to explain!) 41 @type target_object: MY_GameObject 42 @param target_object: the object you want the orientation to (dont know how to explain!) 43 44 @rtype: String 45 @return: returns a string that represents the direction where to find the target object (top, bottom, left, right, front, back or none) 46 """ 47 48 source_pos = source_object.getPosition() 49 source_plane = source_object.worldPlane 50 target_pos = target_object.getPosition() 51 target_plane = target_object.worldPlane 52 53 side = "none" 54 55 if (source_plane == 0 and target_plane == 1) or (source_plane == 1 and target_plane == 0) or (source_plane == 2 and target_plane == 0): 56 in_front_of_me = 0 57 58 if source_plane == 0: 59 if target_pos[1] > source_pos[1]: 60 in_front_of_me = 1 61 elif source_plane == 1 or source_plane == 2: 62 if target_pos[2] > source_pos[2]: 63 in_front_of_me = 1 64 65 if in_front_of_me: 66 side = "front" 67 else: 68 side = "back" 69 70 elif (source_plane == 0 and target_plane == 2) or (source_plane == 1 and target_plane == 2) or (source_plane == 2 and target_plane == 1): 71 left_of_me = 0 72 73 if source_plane == 0 or source_plane == 1: 74 if target_pos[0] < source_pos[0]: 75 left_of_me = 1 76 elif source_plane == 2: 77 if target_pos[1] > source_pos[1]: 78 left_of_me = 1 79 80 if left_of_me: 81 side = "left" 82 else: 83 side = "right" 84 85 elif source_plane == target_plane: 86 87 if source_plane == 0: 88 if target_pos[2] < source_pos[2]: 89 side = "top" 90 else: 91 side = "bottom" 92 elif source_plane == 1: 93 if target_pos[1] < source_pos[1]: 94 side = "back" 95 else: 96 side = "front" 97 elif source_plane == 2: 98 if target_pos[0] < source_pos[0]: 99 side = "left" 100 else: 101 side = "right" 102 return side
103 104 105 ######## Set inter connection between two passive objects
106 -def SetPassivePartsConnection(connected_side, source, target):
107 """ 108 sets the connected list of source and target object, so both know they are connected 109 110 @type connected_side: Integer (0,1,2,3) 111 @param connected_side: the connected side of source (passive) object where the target (passive) object is connected to 112 @type source: MY_GameObject 113 @param source: the object where the target is connected to 114 @type target: MY_GameObject 115 @param target: the object which is connected to source 116 """ 117 118 opposite_side = (connected_side + 2) % 4 119 120 if source.worldPlane == 0: 121 if target.isRotated == 0: 122 opposite_side = 0 123 elif target.isRotated == 1: 124 opposite_side = 2 125 elif source.worldPlane == 1: 126 if connected_side == 0 or connected_side == 2: 127 if target.isRotated == 0: 128 opposite_side = 0 129 elif target.isRotated == 1: 130 opposite_side = 2 131 elif connected_side == 1 or connected_side == 3: 132 if target.isRotated == 0: 133 opposite_side = 3 134 elif target.isRotated == 1: 135 opposite_side = 1 136 elif source.worldPlane == 2: 137 if connected_side == 0 or connected_side == 2: 138 if target.isRotated == 0: 139 opposite_side = 1 140 elif target.isRotated == 1: 141 opposite_side = 3 142 elif connected_side == 1 or connected_side == 3: 143 if target.isRotated == 0: 144 opposite_side = 1 145 elif target.isRotated == 1: 146 opposite_side = 3 147 148 source.setConnectedSide(connected_side, target) 149 target.setConnectedSide(opposite_side, source) 150 151 target.rootConnectObject = source
152 153 154 ######## Get object rotation in same plane as the connected object
155 -def GetNormRotation(direction, plane, up=1):
156 """ 157 gets a normal rotation for an object dependent on the three planes and four direction; in the same plane as the connected object (mostly for stair objects) 158 159 @type direction: Integer (0,1,2,3) 160 @param direction: the direction where an object is connected to 161 @type plane: Integer (0,1,2) 162 @param plane: the plane of the object that makes a normal rotation 163 @type up: Boolean 164 @param up: True if object is turned upwards 165 166 @rtype: Matrix 167 @return: returns the rotation matrix of a 'normal' rotation 168 """ 169 170 ninety = 90 171 oneighty= 180 172 173 angle_x = 0 174 angle_y = 0 175 angle_z = 0 176 angle = 0 177 178 if not up: 179 angle = oneighty 180 181 rot_up = RotationMatrix(angle, 3, "y") 182 183 if (plane == 0): 184 if (direction == 0): 185 pass 186 elif (direction == 1): 187 angle_z = ninety 188 elif (direction == 2): 189 angle_z = oneighty 190 elif (direction == 3): 191 angle_z = -ninety 192 193 elif (plane == 1): 194 if (direction == 0): 195 angle_y = oneighty 196 angle_x = -ninety 197 elif (direction == 1): 198 angle_z = ninety 199 angle_x = ninety 200 elif (direction == 2): 201 angle_x = ninety 202 elif (direction == 3): 203 angle_z = -ninety 204 angle_x = ninety 205 206 elif (plane == 2): 207 if (direction == 0): 208 angle_z = -ninety 209 angle_y = -ninety 210 elif (direction == 1): 211 angle_z = oneighty 212 angle_y = -ninety 213 elif (direction == 2): 214 angle_z = ninety 215 angle_y = -ninety 216 elif (direction == 3): 217 angle_y = -ninety 218 219 rot_x = RotationMatrix(angle_x, 3, "x") 220 rot_y = RotationMatrix(angle_y, 3, "y") 221 rot_z = RotationMatrix(angle_z, 3, "z") 222 223 tot_rot = ((rot_x * rot_y) * rot_z) * rot_up 224 225 return tot_rot
226 227 228 ######## Set object loaction
229 -def GetPlatfNormTransDelta(cur_pos, direction, plane, o_width, direct=0):
230 """ 231 gets the position of a platform by a normal translation with an offset (because of creepy center points) 232 233 @type cur_pos: Vector 234 @param cur_pos: the object's current position 235 @type direction: Integer (0,1,2,3) 236 @param direction: the direction of the object where it is connected to 237 @type plane: Integer (0,1,2) 238 @param plane: the plane of the object that makes the TransDelta 239 @type o_width: Integer 240 @param o_width: the width of an object in the xy-plane; only for platforms!!! 241 @type direct: Boolean 242 @param direct: True if the object dont have to make a translation (for example: only one platform will be spawned in the game) 243 244 @rtype: Vector 245 @return: the new position after a normal translation with a delta 246 """ 247 248 if direct: 249 o_width = 0 250 251 pos_delta = [] 252 253 new_pos = cur_pos[:] 254 255 if (plane == 0): 256 if (direction == 0): 257 new_pos[1] += o_width 258 elif (direction == 1): 259 new_pos[0] += o_width 260 elif (direction == 2): 261 new_pos[1] -= o_width 262 elif (direction == 3): 263 new_pos[0] -= o_width 264 265 elif (plane == 1): 266 if (direction == 0): 267 new_pos[2] += o_width 268 elif (direction == 1): 269 new_pos[0] += o_width 270 elif (direction == 2): 271 new_pos[2] -= o_width 272 elif (direction == 3): 273 new_pos[0] -= o_width 274 275 elif (plane == 2): 276 if (direction == 0): 277 new_pos[2] += o_width 278 elif (direction == 1): 279 new_pos[1] -= o_width 280 elif (direction == 2): 281 new_pos[2] -= o_width 282 elif (direction == 3): 283 new_pos[1] += o_width 284 285 return new_pos
286 287 288 ######## Set object loaction
289 -def GetPlatfVertTransDelta(cur_pos, direction, plane, o_width, o_height, up):
290 """ 291 gets the new position of a platform, when it's rotated 292 293 @type cur_pos: Vector 294 @param cur_pos: the object's current position 295 @type direction: Integer (0,1,2,3) 296 @param direction: the direction of the object where it is connected to 297 @type plane: Integer (0,1,2) 298 @param plane: the plane of the object that makes the TransDelta 299 @type o_width: Integer 300 @param o_width: the width of an object in the xy-plane; only for platforms!!! 301 @type o_height: Integer 302 @param o_height: the height of an object in the xy-plane; only for platforms!!! 303 @type up: Boolean 304 @param up: True if object is turned upward, False when downward (this 'def' is for platforms that are already defined as rotated) 305 306 @rtype: Vector 307 @return: returns the new position after a translation with delta of an already rotated object 308 """ 309 310 new_pos = cur_pos[:] 311 312 t_height = o_height + o_width 313 314 if (up): 315 sign = 1 316 else: 317 sign = -1 318 319 if (plane == 0): 320 new_pos[2] += sign * t_height 321 322 if (direction == 0): 323 new_pos[1] -= o_height 324 elif (direction == 1): 325 new_pos[0] -= o_height 326 elif (direction == 2): 327 new_pos[1] += o_height 328 elif (direction == 3): 329 new_pos[0] += o_height 330 331 elif (plane == 1): 332 new_pos[1] += sign * t_height 333 334 if (direction == 0): 335 new_pos[2] -= o_height 336 elif (direction == 1): 337 new_pos[0] -= o_height 338 elif (direction == 2): 339 new_pos[2] += o_height 340 elif (direction == 3): 341 new_pos[0] += o_height 342 343 elif (plane == 2): 344 new_pos[0] += sign * t_height 345 346 if (direction == 0): 347 new_pos[2] -= o_height 348 elif (direction == 1): 349 new_pos[1] += o_height 350 elif (direction == 2): 351 new_pos[2] += o_height 352 elif (direction == 3): 353 new_pos[1] -= o_height 354 355 return new_pos 356 357 358 ######## Set object rotation
359 -def GetVertRotation(direction, plane):
360 """ 361 gets the rotation for an object if it is rotated (and changed to a new plane) 362 363 @type direction: Integer (0,1,2,3) 364 @param direction: the direction of the object where it is connected to 365 @type plane: Integer (0,1,2) 366 @param plane: the plane of the object that makes this rotation 367 368 @rtype: Matrix 369 @return: returns the rotation matrix of a rotation when an object was already defined as rotated 370 """ 371 372 ninety = 90 373 oneighty= 180 374 375 angle_x = 0 376 angle_y = 0 377 378 if (plane == 0): 379 if (direction == 0 or direction == 2): 380 angle_x = ninety 381 elif (direction == 1 or direction == 3): 382 angle_y = ninety 383 384 elif (plane == 1): 385 if (direction == 0 or direction == 2): 386 pass 387 elif (direction == 1 or direction == 3): 388 angle_y = ninety 389 390 elif (plane == 2): 391 if (direction == 0 or direction == 2): 392 pass 393 elif (direction == 1 or direction == 3): 394 angle_x = ninety 395 396 rot_x = RotationMatrix(angle_x, 3, "x") 397 rot_y = RotationMatrix(angle_y, 3, "y") 398 399 tot_rot = rot_x * rot_y 400 401 return tot_rot
402