OpenClonk
C4BltTransform.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 #include "C4Include.h"
20 
21 void C4BltTransform::SetRotate(float iAngle, float fOffX, float fOffY) // set by angle and rotation offset
22 {
23  // iAngle is in degrees (cycling from 0 to 360)
24  // determine sine and cos of reversed angle in radians
25  // fAngle = -iAngle * pi/180 = iAngle * -pi/180
26  float fAngle = iAngle * -0.0174532925f;
27  float fsin = sinf(fAngle); float fcos = cosf(fAngle);
28  // set matrix values
29  mat[0] = +fcos; mat[1] = +fsin; mat[2] = (1-fcos)*fOffX - fsin*fOffY;
30  mat[3] = -fsin; mat[4] = +fcos; mat[5] = (1-fcos)*fOffY + fsin*fOffX;
31  mat[6] = 0; mat[7] = 0; mat[8] = 1;
32  /* calculation of rotation matrix:
33  x2 = fcos*(x1-fOffX) + fsin*(y1-fOffY) + fOffX
34  = fcos*x1 - fcos*fOffX + fsin*y1 - fsin*fOffY + fOffX
35  = x1*fcos + y1*fsin + (1-fcos)*fOffX - fsin*fOffY
36 
37  y2 = -fsin*(x1-fOffX) + fcos*(y1-fOffY) + fOffY
38  = x1*-fsin + fsin*fOffX + y1*fcos - fcos*fOffY + fOffY
39  = x1*-fsin + y1*fcos + fsin*fOffX + (1-fcos)*fOffY */
40 }
41 
43 {
44  // calc inverse of matrix
45  float det = r.mat[0]*r.mat[4]*r.mat[8] + r.mat[1]*r.mat[5]*r.mat[6]
46  + r.mat[2]*r.mat[3]*r.mat[7] - r.mat[2]*r.mat[4]*r.mat[6]
47  - r.mat[0]*r.mat[5]*r.mat[7] - r.mat[1]*r.mat[3]*r.mat[8];
48  if (!det) { Set(1,0,0,0,1,0,0,0,1); return false; }
49  mat[0] = (r.mat[4] * r.mat[8] - r.mat[5] * r.mat[7]) / det;
50  mat[1] = (r.mat[2] * r.mat[7] - r.mat[1] * r.mat[8]) / det;
51  mat[2] = (r.mat[1] * r.mat[5] - r.mat[2] * r.mat[4]) / det;
52  mat[3] = (r.mat[5] * r.mat[6] - r.mat[3] * r.mat[8]) / det;
53  mat[4] = (r.mat[0] * r.mat[8] - r.mat[2] * r.mat[6]) / det;
54  mat[5] = (r.mat[2] * r.mat[3] - r.mat[0] * r.mat[5]) / det;
55  mat[6] = (r.mat[3] * r.mat[7] - r.mat[4] * r.mat[6]) / det;
56  mat[7] = (r.mat[1] * r.mat[6] - r.mat[0] * r.mat[7]) / det;
57  mat[8] = (r.mat[0] * r.mat[4] - r.mat[1] * r.mat[3]) / det;
58  return true;
59 }
60 
61 void C4BltTransform::TransformPoint(float &rX, float &rY) const
62 {
63  // apply matrix
64  float fW = mat[6] * rX + mat[7] * rY + mat[8];
65  // store in temp, so original rX is used for calculation of rY
66  float fX = (mat[0] * rX + mat[1] * rY + mat[2]) / fW;
67  rY = (mat[3] * rX + mat[4] * rY + mat[5]) / fW;
68  rX = fX; // apply temp
69 }
bool SetAsInv(C4BltTransform &rOfTransform)
void SetRotate(float iAngle, float fOffX, float fOffY)
void Set(float fA, float fB, float fC, float fD, float fE, float fF, float fG, float fH, float fI)
void TransformPoint(float &rX, float &rY) const