Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I'm developing a game called PongBreakout in c++ using a game engine i got from uni (when i was there..) I've got a few problems with my game that i'm struggling to fix and need some help to figure out how to do so.

the first bug is that the countdown is far to fast and i dont know how to slow it down - ive been told to use a scalar (not a clue what that is..) <- ball.cpp (Update())

the code for this is below, I've debugged the value for .mdframetime and it keeps varying between 0.016 and 0.015 however it remains to countdown far to fast.

double frametime = g_theTimer.mdFrameTime; // Update frametime
if (reset == true) // If the ball needs reseting.
{
    countdown -= frametime; // Reduce countdown - This needs fixing its far to fast.
    if(countdown > 2)
    {
        pTheDrawEngine->WriteText(512,384,"3");
    }
    else if(countdown > 1)
    {
        pTheDrawEngine->WriteText(512,384,"2");
    }
    else if(countdown > 0)
    {
        pTheDrawEngine->WriteText(512,384,"1");
    }
    if(countdown <= 0)
    {   
        countdown = 3;
        reset = false;
        position.set(512, 384);
    }
}

the second bug is that the ball sometimes gets stuck at the side of the screen if the paddle is there (play it and you'll eventually notice it.) <- ball.ccp (ProcessCollisons())

void Ball::PanelCollision(Rectangle2D Panel1, Rectangle2D Panel2, Rectangle2D Panel3)
{
if (BallOutline.Intersects(Panel1))
{
    velocity.XValue *= -1;
    velocity.YValue = 5 * ((position.YValue -  Panel1.GetCentre().YValue) / 25);
}
else if (BallOutline.Intersects(Panel2))
{
    velocity.XValue *= -1;
    velocity.YValue = 5 * ((position.YValue -  Panel2.GetCentre().YValue) / 25);
}
else if (BallOutline.Intersects(Panel3))
{
    velocity.XValue *= -1;
    velocity.YValue = 5 * ((position.YValue -  Panel3.GetCentre().YValue) / 25);
}
}

I'm not to sure about whats wrong with this function, as it works untill the panels are at positon 0 or 768 it causes the ball to stick and in some cases go of the screen but still be drawn in the same place.

Do you think its possibly best to make a counter that will increase, after x ammount of times its increased the ball is obviously stuck so get the position of the panel and the positition of the panel, take them away to get the value of how close the ball is stuck towards the center of the panel and minus it away to unstick it?

the third bug is that the rand() function doesnt seem to be working for getting a value between 8 and 5. <- ball.cpp

srand (time(NULL)); // Seed the random
switch(rand() % 4 + 1) // Which way should the ball shoot off?
{
    case 1: velocity.set(rand()%4 + 5 ,-rand() % 5 + 1); // Rand is broken. Why? - Needs to be between 5 and 7. for X
    break;
    case 2: velocity.set(-rand()%4 + 5 ,rand() % 5 + 1);
    break;
    case 3: velocity.set(rand()%4 + 5 ,rand() % 5 + 1);
    break;
    case 4: velocity.set(-rand()%4 + 5 ,-rand() % 5 + 1);
    break;
}

the rand() function needs to return a value between 5 and 7 but its returning values less than 5 in some cases and i cant figure out why. ive tried the max - min + min method and that didn't work either.

To run this game you need direct x aug 2009.

https://www.dropbox.com/sh/yykud3daabght8y/aejKK5bXFZ

http://www.microsoft.com/en-us/download/details.aspx?id=23549

Everything related to my code is on on my dropbox.

share|improve this question
2  
Hi and welcome! You have posted three completely different problems in one question; you should split them up in three different questions. That is how Stack Exchange works; people will be able to understand your specific problem more easily, provide answers more easily and you can pick the right answer for every individual question :) –  soimon Mar 17 '13 at 22:21
add comment

closed as too localized by Sam Hocevar, bummzack, Trevor Powell, Byte56, Sean Middleditch Mar 20 '13 at 1:20

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 2 down vote accepted

First problem: You'll have to either created fixed timesteps for your updates or reduce the countdown based on your delta time (i.e. the time passed). For example, if 1 second passed, you'll have to reduce the countdown by 1. if only 50 ms passed, you'll have to reduce the countdown by 0.05.

Second problem: You not only have to adjust the ball's velocity or making it bounce on collisions. You also have to ensure to move it out of collideables (because sometimes it's just stuck too deep to get out the next Iteration).

Third problem: Although I haven't tried to run the code, I'd say your Problem might be operator precedence. For example, the - in -rand() will negate the return value first. Try to use brackets to group the operations the way you want them to work (e.g. -((rand() % 4) + 5)). Also make sure to not set your seed every Iteration. The return value of time() won't necessarily change every Iteration, as such you might start with the same seed over and over again (depending on the actual timing). Call srand() only once on application startup (or when you really want to set a seed, e.g. when doing replays).

General advice: As mentioned in the comments. Feel free to split such questions and create one post per question. Noone will call you "spammer" here, if all your questions are real questions.

share|improve this answer
    
Problem 2 and 3 fixed, just got 1 to try - thank you and ill be sure to do that next time :). –  Andy Mar 18 '13 at 11:02
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.