1 / 13

Day 6. Dev-related Math Stuff

Day 6. Dev-related Math Stuff. 정보통신학과 송형돈. 목 차. 랜덤 함수 고정 소수점 연산 탄도 궤도와 각도 중력 사용 Sprite 겹침 FPS(F rames Per Second ) 얻기 기타 함수. 랜덤 함수. PA_Rand () returns a HUGE number PA_RandMax (max) returns a number between 0 and N PA_RandMinMax ( min,max )

nowles
Download Presentation

Day 6. Dev-related Math Stuff

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 6. Dev-related Math Stuff 정보통신학과 송형돈

  2. 목 차 랜덤 함수 고정 소수점 연산 탄도 궤도와 각도 중력 사용 Sprite 겹침 FPS(Frames Per Second )얻기 기타 함수

  3. 랜덤 함수 • PA_Rand() • returns a HUGE number • PA_RandMax(max) • returns a number between 0 and N • PA_RandMinMax(min,max) • returns a number from N1 to N2 inclusive • PA_InitRand() • Once run • start the game at the exact same date and time

  4. 랜덤 함수 예시 u32 num = PA_Rand(); u32 max = 4;u32 num = PA_RandMax(max); u32 min = 1;u32 max = 4;u32 num = PA_RandMinMax(min,max);

  5. 고정 소수점 연산(1) s32 speed1 = 256; s32 spritex1 = 0; while(1) // Infinite loops{ spritex1 += speed1; PA_SetSpriteX(0, 0, spritex1>>8); } s16 speed1 = 32; s16 spritex1 = 0; while(1) // Infinite loops{ ppritex1 += speed1; PA_SetSpriteX(0, 0, spritex1>>5); } • Sprite를 움직이기를 원할 경우, 0.5 처럼 소수값으로 사용할 경우 • 스크린의 가로 픽셀은 0 ~ 255 • PA_SetSpriteX(screen, sprite, x) • Sprite을 새로운 위치 X 로이동

  6. 고정 소수점 연산(1) s32 speed1 = 256; s32 spritex1 = 0; while(1) // Infinite loops{ spritex1 += speed1; PA_SetSpriteX(0, 0, spritex1>>8); } 1픽셀씩 이동 s32 speed1 = 128; s32 spritex1 = 0; while(1) // Infinite loops{ ppritex1 += speed1; PA_SetSpriteX(0, 0, spritex1>>8); } 0.5픽셀씩 이동

  7. 중력 사용 • 수많은 게임에서 사용되는 코드 • Sprite 는 수직 속도를 가짐 • 중력의 의미 • 매 턴마다 속도는 가속도에 따라 변경 • 매 턴마다 sprite 위치는 속도에 따라 변경 • 언제든 플레이어가 아래쪽을 터치하면 속도는 null 이 됨

  8. 탄도 궤도와 각도(1) • PAlib 각도 • PA_GetAngle(x1, y1, x2, y2) • 2개의 주어진 위치와 수평선에 의해 만들어진 각도를 반환

  9. 탄도 궤도와 각도(2) • Sin, Cos 함수 • PA_Sin, PA_Cos함수들은 -1~1 사이값을반환 않고 -256~256 값을 반환 • 고정소수점 작업을 위해 • 수평 좌표 위치 Cos • 수직 좌표가 Sin • 주어진 궤도에 따라 움직이게할수 있음

  10. Sprite 겹침(1) • 원형 겹침 • 원 중앙에서 거리를 계산하여 r1 + r2 보다 작은 거리는 충돌

  11. Sprite 겹침(2) • 사각형 겹침 • 원형 겹침과 같은 방법으로 작동하지 않음

  12. FPS(Frames Per Second )얻기 DS의 비디오 화면은 60Hz로 갱신 게임루프 안에서 코드가 실행하는데 16.7ms 보다 더 걸리지 않으면 60 fps가 됨

  13. 기타 함수 • % 대신에 & 사용 • x%2;   ->   x&1; • x%16;  ->   x&15; • bit 시프트 사용 (<< 와 >>) • 2«1 = 4 (2*2^1 = 2*2 = 4) • 2«3 = 16 (2*2^3 = 2*8 = 16) • if/else 최적화 • if/else 문은 비경제적이 • (if levelup==true) x++;  //add one to x • x+=(levelup==true);

More Related