Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game...

13
Game Programming I Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1 Vector Algebra 2 nd Week, 2007 Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1 Downloading DirectX 9.0 SDK

Transcript of Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game...

Page 1: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Game Programming I

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Vector Algebra

2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Downloading DirectX 9.0 SDK

Page 2: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sample Programs (1)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sample Programs (2)

“Book2Part1Code.zip”

Page 3: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Microsoft Visual Studio .Net

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

File New Project…

Visual C++ Win32Win32 Consol Project

Page 4: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Application Settings

Empty project

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

‘Chap01’ Solution

Page 5: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (1)

Copying the source code

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (2)

Project Add Existing Item

Page 6: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (3)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Linking the Library Files

Project PropertiesConfiguration Properties Linker Input

Additional Dependencies : d3dx9.lib

Page 7: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Result

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

D3DXVECTOR3 Class

Page 8: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Useful Functions

Computing the length of a vector

Normalizing a vector

Computing the dot product of two vectors

Computing the cross product of two vectors

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

D3DXVec3 – “main.cpp” (1)#include <d3dx9.h>#include <iostream>using namespace std;

// Overload the "<<" operators so that we can use cout to // output D3DXVECTOR3 objects.

ostream& operator<<(ostream& os, D3DXVECTOR3& v){

os << "(" << v.x << ", " << v.y << ", " << v.z << ")";return os;

}

int main(){

// Using constructor, D3DXVECTOR3(FLOAT x, FLOAT y, FLOAT z);D3DXVECTOR3 u(1.0f, 2.0f, 3.0f);

// Using constructor, D3DXVECTOR3(CONST FLOAT *);float x[3] = {-2.0f, 1.0f, -3.0f};D3DXVECTOR3 v(x);

// Using constructor, D3DXVECTOR3() {};D3DXVECTOR3 a, b, c, d, e;

// Vector addition: D3DXVECTOR3 operator + a = u + v;

Page 9: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

D3DXVec3 – “main.cpp” (2)// Vector subtraction: D3DXVECTOR3 operator -b = u - v;

// Scalar multiplication: D3DXVECTOR3 operator * c = u * 10;

// ||u||float length = D3DXVec3Length(&u);

// d = u / ||u||D3DXVec3Normalize(&d, &u);

// s = u dot vfloat s = D3DXVec3Dot(&u, &v);

// e = u x vD3DXVec3Cross(&e, &u, &v);

cout << "u = " << u << endl;cout << "v = " << v << endl;cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;cout << "d = " << d << endl;cout << "e = " << e << endl;cout << "||u|| = " << length << endl;cout << "u dot v = " << s << endl;

return 0;}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Example 1.4

Let and . Find the angle between and .

( )3,2,1=ur ( )1,0,4 −−=v

r

ur

vr

θcosvuvurrrr

=⋅

( ) ( )( ) ( )

1714

7

104321

130241

cos

222222

222222

−=

−++−++

−⋅+⋅+−⋅=

++++

++=

⋅=

zyxzyx

zzyyxx

vvvuuu

vuvuvu

vu

vurr

rr

θ

°≈⎟⎠

⎞⎜⎝

⎛ −=∴ − 117

1714

7cos 1θ

Page 10: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Example 1.4 – Program

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Example 1.4 – Result

Page 11: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Example 1.6

Let and . Compute and , and verify that is orthogonal to and that is orthogonal to .

( )3,1,2=ur ( )0,0,2=v

rvuwrrr

×=wr

ur

vr

( ) ( ) ( )( )zyyxzxxzyzzy vuvuvuvuvuvuvu −−−=× ,,rr

( ) ( ) ( )( )( )2,6,0

2102,0223,0301

−=⋅−⋅⋅−⋅⋅−⋅=w

r

( ) 0321620 =⋅−+⋅+⋅=⋅uwrr

( ) 0020620 =⋅−+⋅+⋅=⋅vwrr

uvyrrr

×=wr

( ) ( ) ( )( )( )2,6,0

2012,3220,1030

−=⋅−⋅⋅−⋅⋅−⋅=y

r

∴ is orthogonal to and .wr

ur

vr

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Example 1.6 – Program

Page 12: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Example 1.6 – Result

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Exercise

Program the following questions:

(Exercise 4) Is the angle between and orthogonal, acute, or obtuse?

(Exercise 10) Let the following points define a triangle relative to some coordinate system: A=(0,0,0), B=(0,1,3), and C=(5,1,0). Find a vector orthogonal to this triangle. (Hint: Find two vectors on two of the triangle’s edges and use the cross product.)

ur

vr

( ) ( )2,2,2 ,1,1,1 == vurr

( ) ( )0,2,2 ,0,1,1 −== vurr

( ) ( )0,1,3 ,1,1,1 =−−−= vurr

Page 13: Vector Algebra - Hallymgraphics.hallym.ac.kr/teach/2007/gp1/src/02prac.pdf · 2008. 9. 16. · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Vector Algebra 2nd Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 (1)

다음질문의프로그램을작성하여답하시오.

(Exercise 4) 다음각각의두벡터 와 가이루는각이직각인가예각인가둔각인가?

(Exercise 10) 어떤좌표계에서세점 A=(0,0,0), B=(0,1,3), C=(5,1,0)가한삼각형을정의한다고하자. 이삼각형에수직인벡터를구하시오. (힌트: 삼각형모서리에있는두개의벡터를구한다음, 외적을이용한다.)

ur

vr

( ) ( )2,2,2 ,1,1,1 == vurr

( ) ( )0,2,2 ,0,1,1 −== vurr

( ) ( )0,1,3 ,1,1,1 =−−−= vurr

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 (2)

제출방법2007. 3. 15수업시간전까지

Exercise 4와 10에대한프로그램각각을작성 (예: “ex04.cpp”와 “ex10.cpp”) + 실행결과 (본강의자료20쪽또는 23쪽과같은출력이미지, 예: “ex04.jpg”와“ex10.jpg”)

4개파일을압축한 “학번.zip”을웹에 upload 할것