Download presentation
Presentation is loading. Please wait.
Published byTyler Perry Modified over 8 years ago
1
Mathematics for Game Programming (1)3 인칭 카메라 (2)Particle dynamics (3)quaternion
2
Camera part3 n n Take a look the functions for camera controls n n Implement a suitable function for demand Quaternion Take a look the functions using quaternions Implement the functions for quaterions 회전변환과 quaternion 의 관계 Particle Dynamics 자유낙하하는 공의 운동. 땅에 부딪힌 후의 움직임
3
실습프로그램 소개 (1)Skybox : skybox 가 무엇인지, 어떻게 구성되어 있는지 살펴보기. 참고로만 볼 예정 (2) Camera_part3: 3 인칭 카메라에 대한 예. 중심이 되는 object 에 대한 카메라의 움직임에 대한 함수 이해. 충돌 부분은 빠져 있음 중심이 되는 object 에 대한 카메라의 움직임에 대한 함수 이해. 충돌 부분은 빠져 있음
4
(3) Bouncing _ball: 자유 낙하하는 물체의 운동. 초기 속도에 따라 튀는 방향이 달라짐. 바닥이 평면인 경우만 구현. 바닥의 모양이 평면이 아닌 경우 어떻게 해야 할까 ? (4)rotation: 회전 변환이 여러 번 나온 간단한 예. 목표는 이 프로그램이 아니라 회전 변환을 여러 번 수행하는 경우 quaternion 을 이용하여 똑같은 결과를 얻는다.
5
Representations of Rotation n Euler Angles (e.g. roll, pitch, yaw) n Rotation Matrices n Axis and angle n Unit Quaternions
6
Representations of Rotation n Need to represent transformation relative to some fixed coordinate system n In 2D, use one angle: p p’ convention: postive values for represent counter-clockwise rotations
7
3D Rotation n Three degrees of freedom for rotations in 3D n Intuitively, camera rotations require two DOF for direction of gaze and one DOF for tilt angle: azimuth elevation tilt y x z
8
n Use a set of three Euler angles , χ, φ n Example: –rotate about z axis by –about y axis by χ –finally about x axis by φ Euler Angles y xzy xzy x z χ φ
9
n Definition: angles representing combinations of three successive rotations about axes, which can effect arbitrary 3D rotation n Total of 24 conventions (12 fixed axes, 12 relative axes) n Not all combinations of axes can effect arbitrary 3D rotation (we need at least two distinct axes).
10
Effecting 3D Rotation using only 2 axes y xzy xz y xzy xzy x x 90 90 90 z b b b b 90 effective rotation
11
Rotation Matrices n Rotation about z axis by x z y ( cos , sin , 0 ) ( - sin , cos , 0 ) ( 0, 1, 0 ) ( 1, 0, 0 ) ( 0, 0, 1 )
12
Rotation Matrices n The effect of a rotation matrix is apparent by considering how it transforms the unit axes : x zy ( cos , sin , 0 ) ( - sin , cos , 0 ) ( 0, 1, 0 ) ( 1, 0, 0 ) ( 0, 0, 1 )
13
Rotation Matrices about axes n Rotation about x axis by χ (DirectX 에서 sin 부호 바뀜 ) n Rotation about y axis by φ (…) n Rotation about z axis by (…) (…)
14
DirectX 에서 회전행렬 사용시 주의점 DirectX 는 왼손 좌표계를 사용한다. 회전축이 X,Y, 또는 Z 축일 때 각 a 가 주어져 있을 때 회전이란 회전축을 바라보고 시계방향으로 주어진 각만큼 회전하는 것으로 정의한다. 즉 왼손으로 축을 감싸고 돌리는 것으로 정의한다. 그러므로 오른손 좌표계를 쓰는 경우에서 –a 만큼 회전하는 것으로 생각할 수 있다.
15
General Rotation Matrix n Unit vectors along axes map to mutually perpendicular, unit vectors : n For right-handedness, we require : det (R) > 0 for i = j otherwise
16
Fomular for rotation matrix for given axis A(unit vector) and angle n (3.21) P89 (C = cos θ, S = sin θ)(camera_3 의 camera.cpp 참고 ) n 예를 들어 θ =120°, 축은 (1,1, 1) 이라 하자 (1, 1, 1) 을 정규화 시키면 (1, 1, 1) 을 정규화 시키면
17
n D3DXMatrixRotationX n D3DXMATRIX R; n D3DXVECTOR3 axis(0.707f,0.707f,0.707f); n D3DXMatrixRotationAxis(&R,&axis, D3DX_PI/2.0f*4.0/3.0); D3DXVECTOR3 v( 1.0f, 1.0f, 0.0f); D3DXVECTOR3 v( 1.0f, 1.0f, 0.0f);D3DXVecTransformCoord(&v,&v,&R);
18
If we want to apply several transformation(rotation, sear, scaling, reflection, and translation ets…), we just multiply the suitable matrix in order. Also we need to use homogeneous coordinate because translation is not linear in ordinary coordinates.
19
한 점을 중심으로 회전할 때 n 중심점을 C(c1,c2,c3), 회전축을 A(a1,a2,a3), 회전각을 angle, 회전하고자 하는 점을 P(p1,p2,p3), P’ 는 회전 후의 P 의 위치라 하면 다음과 같은 과정으로 P’ 를 얻는다. n (1)P=P-C n (2) (1) 의 P 를 A 와 angle 에 따라 회전한다. n (2) (2) 의 P 에 C 를 더한다. P=P+C
20
Camera part3 n void CCamera::rotateY(float angle, const CPos &target) n { n //d3d9 과 d3dx lib 를 이용하여 camera 의 멤버와 함수를 D3DXVECTOR3 와 D3DXMATRIX 를 사용하여 수정하면 n // 간단해짐. 예를 들어 다음처럼 n /* n D3DXMATRIX T; n D3DXMatrixRotationY(&T,angle); n D3DXVec3TransformCoord(&eye,&eye,&T); n n eye+=target; n setTarget(target); n */ n //target 을 중심으로 camera 의 eye 를 y 축으로 angle 만큼 회전
21
n float xxx, zzz; n float sinAng, cosAng; n n // Get sin()/cos() of angle n sinAng = sinf(angle); n cosAng = cosf(angle); n n // First translate to the world's origin n eye -= target; n n // Save off forward components for computation n xxx = eye.x; n zzz = eye.z; n n // Rotate forward vector n eye.x = xxx * cosAng + zzz * sinAng; n eye.z = xxx * -sinAng + zzz * cosAng; n n // Translate back n eye += target; n n setTarget(target); // Build the camera's axes n
22
void CCamera::pitch(float angle, const CPos &target) n // 주의 :1 학기 교과서의 pitch 와 다름. 주어진 target( 위치 ) 와 right(one of the local direction of camera) 에 따라 eye 와 forward 가 바뀌므로 다른 local direction 들도 바뀐다. n // 쉬운 예로 target 이 원점이고 right 가 (1,0,0) 인 경우 상상해 보시오. n //taget 과 eye 는 일정한 거리를 유지하도록 조절한다. n 이 함수 위에 있는 행렬은 주어진 축과 각에 대해 시계 반대 방향으로 회전 하는 행렬이다.
23
Character Animation in Games n A common theme in many action and adventure, sports, RPG puzzle, and fighting video games Tomb Raider 3 (Eidos Interactive)Final Fantasy VIII (SquareSoft)The Legend of Zelda (Nintendo)
24
Updating Character Kinematics Faster n Quaternion vs. 3D Transformation Matrices For rotation composition: Quat/Vec pair is more efficient (also smaller data to store: 7 floats vs 12 floats for matrices) For point/vector transformation: Matrices are more efficient n The usual practice: –Use quaternions when composing long sequences of rotations –Convert to homogeneous transformation matrices in order to transform lots of points / vectors / triangles
25
quaternion n Q = = w + xi + yj + zk Q = w+ v Q = w+ v 다음과 같이 곱셈을 정의하고 다음과 같이 곱셈을 정의하고 i 2 = j 2 = k 2 = -1 i 2 = j 2 = k 2 = -1 ij = -ji = k ij = -ji = k jk = -kj = i jk = -kj = i 분배법칙을 이용하면 분배법칙을 이용하면 Q 1 = w 1 + x 1 i + y 1 j + z 1 k Q 2 = w 2 + x 2 i + y 2 j + z 2 k 의 Q 1 = w 1 + x 1 i + y 1 j + z 1 k Q 2 = w 2 + x 2 i + y 2 j + z 2 k 의 곱 Q 1 Q 2 는 다음과 같다. 곱 Q 1 Q 2 는 다음과 같다.
26
Q 1 Q 2 = (w 1 w 2 – x 1 x 2 – y 1 y 2 – z 1 z 2 ) + (w 1 x 2 + x 1 w 2 + y 1 z 2 – z 1 y 1 )i + (w 1 x 2 + x 1 w 2 + y 1 z 2 – z 1 y 1 )i + (w 1 y 2 – x 1 z 2 + y 1 w 2 + z 1 x 2 )j + (w 1 y 2 – x 1 z 2 + y 1 w 2 + z 1 x 2 )j + (w 1 z 2 + x 1 y 2 – y 1 x 2 + z 1 w 2 )k --- (1) + (w 1 z 2 + x 1 y 2 – y 1 x 2 + z 1 w 2 )k --- (1) Q 1 = s 1 + Q 2 s Q 1 = s 1 + ν 1 Q 2 = s 2 + ν 2 Q 1 Q 2 = s 1 s 2 – · + s 1 + s 2 + x Q 1 Q 2 = s 1 s 2 – ν 1 · ν 2 + s 1 ν 2 + s 2 ν 1 + ν 1 x ν 2
27
( 정의 ) Q = s + Q = s + ν 에 대하여 conjugate( 켤레사원수 ) Q = s - 이다. Q = s - ν 이다. 곱셈 (1) 에 의하여 _ _ _ _ QQ = QQ = Q · Q = ||Q|| 2 QQ = QQ = Q · Q = ||Q|| 2
28
( 정리 ) Q 의 역수 Q -1 은 다음과 같다. _ Q -1 = Q / ||Q|| 2 Q -1 = Q / ||Q|| 2 예 ) Q = 이면 ||Q|| 2 = 4 Q -1 = ¼ Q -1 = ¼ => QQ -1 = => QQ -1 =
29
주어진 사원수 Q 에 대하여 φ Q (P) = QPQ -1 라고 하자. P 는 o + v 형태의 사원수, 즉 3 차원 벡터라면 QPQ -1 역시 o + v 형태이다. QPQ -1 역시 o + v 형태이다. 단위벡터, A = (Ax, Ay, Az) θ 에 관하여 Q = cos θ/2 + Asin θ/2 라면 φ Q (P) = QPQ -1 는 P 를 축 A 를 중심으로 θ 만큼 시계 방향으로 회전한 결과이다. Q = cos θ/2 + Asin θ/2 라면 φ Q (P) = QPQ -1 는 P 를 축 A 를 중심으로 θ 만큼 시계 방향으로 회전한 결과이다.
30
(1) 그러므로 주어진 축과 각에 대한 적당한 Q 를 찾아서 quaternion 의 곱으로 벡터를 회전 시킬 수 있다. (2) Q 1 (Q 2 PQ 2 -1 ) Q 1 -1 = (Q 1 Q 2 )P(Q 1 Q 2 ) -1 을 이용하여 여러 회전에 대한 결과를 quaternion 을 곱해서 하나의 회전으로 얻을 수 있다.
31
(3) 다른 변환 ( 이동..) 과 합성할 때 해당하는 quaternion 에 대한 회전 행렬을 이용하여 이 행렬과 다른 변환에 대한 행렬을 곱하여 최종의 변환을 구한다.
32
( 예 ) θ = 120°, A = 일때 이에 대한 quaternion Q
33
n P=(1,-1,0) 을 축 (1,1,1) 에 대하여 120 도 회전한 결과를 R 이라 하자. P 을 인 quaternion 으로 간주하고 Q 와의 quaternion 의 곱 P’=QPQ -1 을 계산한다. Quaternion 으로 P’ 는 실수부가 0, 즉 P’= 이므로 P 를 축 (1,1,1) 에 대하여 120 도 회전한 결과는 R=(a,b,c) 임을 얻는다.(a=0,b=1,c=-1 임을 확인하시오 ) 인 quaternion 으로 간주하고 Q 와의 quaternion 의 곱 P’=QPQ -1 을 계산한다. Quaternion 으로 P’ 는 실수부가 0, 즉 P’= 이므로 P 를 축 (1,1,1) 에 대하여 120 도 회전한 결과는 R=(a,b,c) 임을 얻는다.(a=0,b=1,c=-1 임을 확인하시오 )
34
Quaternion 에 대응되는 회전행렬
35
Q = 이고 ||Q|| 2 = 1, P 는 3 차원 벡터일때 QPQ -1 =MP 로 계산 할 수 있으며 M 은 다음과 같다. M = w = cos θ/2, x = Ax sin θ/2, y = Ay sinθ/2, z = Az sinθ/2 라 하면 이는 유인물 (3.21) 의 행렬과 같다.(x,y,z 수정된 것 주의 ) w = cos θ/2, x = Ax sin θ/2, y = Ay sinθ/2, z = Az sinθ/2 라 하면 이는 유인물 (3.21) 의 행렬과 같다.(x,y,z 수정된 것 주의 )
36
A=(Ax,Ay,Az) D3DXMatrixRotationAxis(&R,&A, θ ) 에 대한 행렬 R 을 얻기 위하여 : 대응되는 quaternion 은 q=cos θ +A(sin θ) 이므로 대응되는 quaternion 은 q=cos θ +A(sin θ) 이므로 w = cos θ/2, x = Ax sin θ/2, y = Ay sinθ/2, w = cos θ/2, x = Ax sin θ/2, y = Ay sinθ/2, z = Az sinθ/2 를 앞의 행렬 M 에 대입하면 된다. z = Az sinθ/2 를 앞의 행렬 M 에 대입하면 된다.
37
n SLERP n Introduction to the Game Camera n Structuring Camera Movement n Problems and Solutions—Enhancements n Thoughts Quaternion Interpolation and Camera Control
38
The Interpolation Problem n In general: If we have two “keyframe” transformations, how do we determine intermediate transformations to generate a smooth transition between them? n Want to interpolate between two different rotations. n What to do if using Euler angles? –Just interpolate individual angles –Works, but intermediate rotations are unpredictable. –“Long way around”
39
Quaternion Interpolation n Standard linear interpolation: –LERP(Q0,Q1,t) = Q0 + t(Q1-Q0) –Possible given defined operators on quats, and results in linear interpolation of all four components. –Intermediate values don’t represent rotations! They aren’t normalized, and don’t fall on the unit hypersphere.
40
Quaternion Interpolation n Revised linear interpolation: BetterLERP(Q0,Q1,t) = normalize(LERP(Q0,Q1,t)) –Rate of interpolation is not constant!
41
SLERP n Spherical Linear intERPolation n Instead of interpolating along a straight line between Q0 and Q1, interpolate along the surface of the unit hypersphere, tracing the arc between them
42
SLERP n SLERP(Q0,Q1,t) = n Interpolates along the arc between the two points on the hypersphere n Need to know θ, the shortest angle between the two quats
43
SLERP n SLERP(Q0,Q1,t) = n Gives us a smooth interpolation (constant rate of change) n When θ is the shortest angle, gives us the shortest logical rotation between the two arbitrary quaternions n Can also do spline interpolation along a sequence of quaternions for a smooth path of rotations
44
Particle dynamics
45
Example: 2D Particle n A single particle moving in a flow field Position:Velocity: The flow field function dictates particle velocity
46
Vector Field n The flow field g(x,t) is a vector field that defines a vector for any particle position x at any time t.
47
n The equation v = g(x, t) is a first order differential equation: n The position of the particle is computed by integrating the differential equation: n Typically this integral cannot be computed analytically. Differential Equations
48
Numeric Integration n Instead we compute the particle’s position by numeric integration: starting at some initial point x(t 0 ) we step along the vector field to compute the position at each subsequent time instant. This type of a problem is called an initial value problem.
49
Euler’s Method n The simplest solution to an initial value problem. Euler’s method starts from the initial value and takes small time steps along the flow: n Consider Taylor series expansion of function x(t):
50
Other Integration Techniques n Euler’s method is the simplest numerical method, but is inaccurate and unstable, requiring small time steps. The error is proportional to n Other methods: –Midpoint (2 nd order Runge-Kutta) –Higher order Runge-Kutta (4 th order, 6 th order) –Adams –Adaptive Stepsize
51
Euler method Second order Euler Midpoint method
52
n What is a motion of a particle in a force field? n The particle moves according to Newton’s Law: n The mass m of a particle describes the particle’s inertial properties: heavier particles are easier to move than lighter particles. In general, the force field f(x, v, t) may depend on the time t and particle’s position x and velocity v. Particle in a Force Field
53
2nd-Order Differential Equations n Newton’s Law yields an ordinary differential equation of second order: n Define a new phase space vector y, which consists of particle’s position x and velocity v, then construct a new first-order differential equation whose solution will also solve the second-order differential equation.
54
자유낙하운동 n Velocity V_0=0( 벡터로 ), n Acceleration a=(0,-g, 0) (g 는 중력상수 ) n V_t=V_0+at n Position n X_t=X_0 +1/2 at^2
55
n V: 바닥에 충돌했을 때 velocity n W=-V/||V|| n C=(x,y,z): 충돌점 N_c 는 C 에서의 법선벡터 N_c 는 C 에서의 법선벡터 r 은 탄성계수라면 (0<=r<=1) r 은 탄성계수라면 (0<=r<=1) 충돌점에서의 새로운 속도는 V’ 는 V’=2*(W 와 N_c 의 내적 )N_c-W V’=2*(W 와 N_c 의 내적 )N_c-WV’=V’*||V||*r
56
탄성계수가 1 보다 작은 경우 결국 속도는 0 으로 된다. 이때 바닥에서 구르는 방향은 접점의 평면에 대한 gradient 이며 마찰계수를 곱하여 계산한다.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.