Hi all!
Ive started making a game, similar to this one:
http://www.newground...tal/view/409137, except im making it with Win32 and C++. Ive made the moving line with dots, that is, every frame a new dot is printed on screen, and because its fast, it seems like its a line. Is there any better way to do that? Also, when steering, if i hold down key 'Q' or 'E' (for left and right), how could i achieve so that there would be a max steering angle, so that if i hold one of these buttons, i would collide with my line, because now when i hold it down, i just keep getting in a more and more tight circle, like so:
http://imageshack.us/photo/my-images/831/circleu.png/. Also, when using steering my velocity keeps getting lower, so im adding code for rotation aswell, if maybe something is wrong with it (im pretty sure this is also the cause of getting into smaller and smaller circle, as shown in the picture above):
Rotation code:
1 2 3 4 5 6
|
Vec2& Vec2::rotate(float t)
{
x = x * cosf(t) - y * sinf(t); //x represents the first component of the Vec2 direction(0.0f,-120.0f);
y = y * cosf(t) + x * sinf(t); //y represents the second component of the Vec2 direction(0.0f,-120.0f);
return *this;
}
| |
Globals and window procedure:
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
|
BackBuffer* gBackBuffer = 0;
const int gClientWidth = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
const int gClientHeight = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
Vec2 player(500.0f,500.0f);
Vec2 direction(0.0f,-120.0f);
float angle = 0.0f;
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc = 0;
switch( msg )
{
case WM_CREATE:
// Create the backbuffer.
gBackBuffer = new BackBuffer(hWnd, gClientWidth,gClientHeight);
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(ghMainWnd);
break;
case 'Q':
direction.rotate(-0.1f);
break;
case 'E':
direction.rotate(0.1f);
break;
}
return 0;
case WM_DESTROY:
delete gBackBuffer;
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
| |
Message and game loop:
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
|
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
// Get the performance timer frequency.
__int64 cntsPerSec = 0;
bool perfExists = QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec)!=0;
if( !perfExists )
{
MessageBox(0, "Performance timer does not exist!", 0, 0);
return 0;
}
double timeScale = 1.0 / (double)cntsPerSec;
// Get the current time.
__int64 lastTime = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&lastTime);
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// Get time
__int64 currTime = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
double deltaTime = (double)(currTime - lastTime) * timeScale;
// backbuffer DC
HDC bbDC = gBackBuffer->getDC();
Vec2 velocity = direction*2.0f;
player += deltaTime*velocity;
// Paint the dot to be controlled
HBRUSH oldBrush = (HBRUSH)SelectObject(bbDC, GetStockObject(BLACK_BRUSH));
Ellipse(bbDC, player.x-4, player.y-4, player.x+4, player.y+4);
SelectObject(bbDC, oldBrush);
gBackBuffer->present();
lastTime = currTime;
}
}
| |