-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformations2d.py
executable file
·108 lines (96 loc) · 4.16 KB
/
transformations2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
''' Various 2D Transform functions
Created for HIT3046 AI for Games by Clinton Woodward cwoodward@swin.edu.au
'''
from matrix33 import Matrix33
#------------------------------------------------------------------------------
def WorldTransformScale(points, pos, forward, side, scale):
''' Transform the given list of points, using the provided position,
direction and scale, to object world space. '''
# make a copy of original points (so we don't trash them)
tran_points = [ pt.copy() for pt in points ]
# create a transformation matrix to perform the operations
matTransform = Matrix33()
# scale (but only if needed)
if (scale.x != 1.0) or (scale.y != 1.0):
matTransform.scale_update(scale.x, scale.y)
# rotate
matTransform.rotate_by_vectors_update(forward, side)
# and translate
matTransform.translate_update(pos.x, pos.y)
# now transform all the points (vertices)
matTransform.transform_vector2d_list(tran_points)
# done
return tran_points
#------------------------------------------------------------------------------
def WorldTransform(points, pos, forward, side):
''' Transform the given list of points, using the provided position and
direction, to objects world space. '''
# make a copy of original points (so we don't trash them)
tran_points = [ pt.copy() for pt in points ]
# create a transformation matrix to perform the operations
matTransform = Matrix33()
# rotate
matTransform.rotate_by_vectors_update(forward, side)
# and translate
matTransform.translate_update(pos.x, pos.y)
# now transform all the points (vertices)
matTransform.transform_vector2d_list(tran_points)
# done
return tran_points
#------------------------------------------------------------------------------
def PointToWorldSpace(point, pos, forward, side):
''' Transforms a point from the agent's local space into world space'''
# make a copy of the point
TransPoint = point.copy()
# create a transformation matrix
matTransform = Matrix33()
# rotate
matTransform.rotate_by_vectors_update(forward, side)
# and translate
matTransform.translate_update(pos.x, pos.y)
# now transform the vertices
matTransform.transform_vector2d(TransPoint)
return TransPoint
#------------------------------------------------------------------------------
def VectorToWorldSpace(vec, forward, side):
''' Transforms a vector from the agent's local space into world space '''
# make a copy of the point
TransVec = vec.copy()
# create a transformation matrix
matTransform = Matrix33()
# rotate
matTransform.rotate_by_vectors_update(forward, side)
# now transform the vertices
matTransform.transform_vector2d(TransVec)
return TransVec
#------------------------------------------------------------------------------
def PointToLocalSpace(point, pos, forward, side):
''' Transform point to local space. '''
# make a copy of the point
TransPoint = point.copy()
# We'll plug the changes straight in a matrix... save some time (perhaps)
Tx = -pos.dot(forward)
Ty = -pos.dot(side)
m = [forward.x, side.x, 0.0, forward.y, side.y, 0.0, Tx, Ty, 1.0]
matTransform = Matrix33(m)
# now transform the vertices
matTransform.transform_vector2d(TransPoint)
return TransPoint
#------------------------------------------------------------------------------
def VectorToLocalSpace(vec, forward, side):
''' Return a new vector with the translated direction '''
# make a copy of the point
TransVec = vec.copy()
# create the transformation matrix and plug values straight in
m = [forward.x, side.x, 0.0, forward.y, side.y, 0.0, 0, 0, 1.0]
matTransform = Matrix33(m)
# now transform the vector
matTransform.transform_vector2d(TransVec)
return TransVec
#------------------------------------------------------------------------------
def Vec2DRotateAroundOrigin(vec, rads):
''' Rotates a vector a given angle (in radians) around the origin.
Note: the vec parameter is altered (does not return a new vector. '''
mat = Matrix33()
mat.rotate_update(rads)
mat.transform_vector2d(vec)