DirectX Movement Problems

Started by
8 comments, last by ereigner 21 years, 10 months ago
My main problem is that c++ is so damn fast that in my game when my ship goes to rotate left or right, it spins so fast that its hard to slow down. Now I have added a Sleep statement to slow it down a little because the game is based on a 1985 dogfight game written in basic. Im looking for a way to slow my movement down. My second problem is my code for my bullets. They come out great at the begging. The bullets are suppose to work just like they do in asteroids, with no more than 4 bullets on the screen at once. The problem is after a few loops threw the cycle the bullets go only half their normal distance.
Advertisement
if you work under windows, try some message loop like this:

DWORD starttime, endtime;
starttime = endtime = timeGetTime ();
while (1)
{
endtime = timeGetTime ();
// DO MESSAGE PROCESSING HERE (PeekMessage!)
if (endtime - starttime > 125)
{
starttime = timeGetTime ();
// DO GAME UPDATE HERE
}
}

in this loop, the game is updated every 125 msec.
in the game update function(s), you can implement a framerate
independent movementcode for your ship, since it is only
called every 125 msecs.
a better (but a bit more complicated) way is this:

while (1)
{
do
{
endtime = timeGetTime();
} while (endtime - starttime < 1);
DoGameUpdate (endtime-starttime);
}

Now you have the function DoGameUpdate with the time
of the last frame. in this function you should implement
game updates based on the time passed to the function.
You need to base all your movement on time stamps.
Use the lowres timer GetTickCount() or the microsecond hires timer functions QueryPerformanceFrequency and QueryPerformanceCounter.

If you want your ship to rotate at (say) 360deg per second, you need to calculate the elapsed time since the last loop and rotate accordingly.
<a href="http://www.purplenose.com>purplenose.com
yea that worked great for my movement, got any ideas on the bullet problem?
Keep an array of bullet object pointers and an index say

CBullet* apBullets[MAX_NUM_BULLETS]
int iBulletIndex=0;

and assign all pointers in array to NULL.
When player fires create a new bullet object at the first NULL pointer found in the array.

When bullets exit the screen delete the object at set pointer to NULL.

<a href="http://www.purplenose.com>purplenose.com
yea i have an array that holds the bullets,
looks like this, first i find the first free bullet
, then i ensure that its correct and their are free bullets available

int number = (NUMBULLETS - first.freebullets);
if(first.freebullets > 0)
/* fire # (NUMBERBULLETS - FREEBULLETS)*/
{
if(first.bullets[number].fired == false)
{
first.bullets[number].dir = first.direction;
first.bullets[number].x = first.x + 4;
first.bullets[number].y = first.y -+ 4;
first.freebullets -= 1;
first.bullets[number].fired = true;
}

this is done right after the user presses the fire button.
this is very simple for right now, the direction is basically made up of 8 quadrants, from 0 to 7
based on this quadrant is how the bullets gets its velocity,
the user can press and fire each button one at a time and it goes the full distance but when you hold down the fire button, all 4 bullets come out at once and only go half the normal distance. To keep tabs of the bullet i only let it move a certain number of movements, in the main loop where i check for bullets and apply their movement is where this number is applied. I know this code is sort of cheesy but its my first C++ directx game, Im mostly a VB programmer, making my way to C++;
I''m not sure what the ''first'' structure is for. Are you updating each bullets position based on the other bullets?

It seems like a bit of an awkward method!

Why not just keep velocity info within each bullet structure (class whatever)


  SBullet{  double dVelocityX;  double dVelocityY;  double dPositionX;  double dPositionY;}  



Each loop just add the velocity components to the position (taking elapsed time into account maybe).
To create a new bullet just initialise the velocity vars according to your quadrant.

Keep an array of pointers to these objects as mentioned earlier, and destroy object when it exits your clipping area (screen probably).
Hope this helps...
<a href="http://www.purplenose.com>purplenose.com
Nah i do do that later in the main loop , i update all the velocities of my players and their bullets all at once. I figured out the answer anyway. Im such a dumbass the solution above for my movement problem will also fix this problem. Thanks anyway.
Got a question about the pointer array, Should the data type be a pointer to the bullet structure??
Its personal choice really.
I''d used array pointers because it saves you havig to have a separate array of flags for available/unavailable bullets (the array entries are just NULL).
<a href="http://www.purplenose.com>purplenose.com

This topic is closed to new replies.

Advertisement