• Advertisement

Archived

This topic is now archived and is closed to further replies.

DirectX Movement Problems

This topic is 5715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++;

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Got a question about the pointer array, Should the data type be a pointer to the bullet structure??

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

  • Advertisement