Incorrect speeds

Started by
5 comments, last by Ganoosh_ 17 years, 7 months ago
In my game, I have some enemies, that basically just generate with a random direction and speed and just bounce around the screen. You, as player, can grab and throws them. This is in 2D by the way. However the speeds are not update correctly or something. They randomly generate between about 300 - 400 (pixels/sec), but move VERY slowly. It's not the timing, because when I grab them and throw them at that speed it's much faster and the same number, or if I just grab and let go, they slowly get back up to speed, and when they reach their designated speed, they're moving much faster. The update code is the same regardless of whether it's just moving or been thrown or just grabbed and let go. Here's the update code:
if (speed > nSpeed) speed -= 100.0f * elapsedTime; // slow down if flying
        if (speed < nSpeed) speed += 8.5f * elapsedTime; // speed up if under start speed
        location += dir * (speed * elapsedTime); // update location
I don't see any problem with it, the numbers themselves are working fine (as it's going to the same number), but until the enemie has been grabbed and has to regain to it's set speed, it moves very slowly. Why could this be happening?
Advertisement
Uh, I think we'll need to see a little more code than that :)
Quote:Original post by Zahlman
Uh, I think we'll need to see a little more code than that :)


lol I guess so. I personally can't find anything, but maybe some one else can.
// constructorEnemy::Enemy(Vector2D loc, int type) : location(loc), dead(false), grabbed(false), type(type) {    // set a random direction using a random xDir and yDir    dir = Vector2D(1.0f/(100.0f-(float(rand()%199+1))), 1.0f/(100.0f-(float(rand()%199+1))));    nSpeed = float(rand()%100+400); // random speed between 400 and 500    speed = nSpeed;    // random rotation between -0.5 and -2 and 0.5 and 2    rot = float(rand()%150+50)/100.0f;    rot = (rand()%1)?rot:-rot;}

The update for the enemy is here:
if (!grabbed) {        if (speed > nSpeed) speed -= 100.0f * elapsedTime; // slow down if flying        if (speed < nSpeed) speed += 8.5f * elapsedTime; // speed up if under start speed        location += dir * (speed * elapsedTime); // update location    } else {        float delta = elapsedTime / 0.02f; // set the decay delta based on elapsed time        if (delta > 1.0f) delta = 1.0f; // limit max speed        location += (gLoc - location) * delta; // move based on direction and speed    }

That's all for the movement the rest is just rotating the sprite and bounds checking so I didn't include it.. Less uselessness to read lol
Grabbing code is here: pLoc is player location geid is grabbed enemy id (index of curr enemy being grabbed)
if (grab) {        if (!Mouse::LMB) { // player released mouse            enemies[geid]->setGrabbed(false); // set grabbed enemy to longer grabbed            if (pLoc != enemies[geid]->getGrabLoc()) {                enemies[geid]->dir = pLoc - enemies[geid]->location;                enemies[geid]->dir.normalize();                Vector2D temp = enemies[geid]->getGrabLoc();                float dist = sqrt(((temp.x-pLoc.x)*(temp.x-pLoc.x))+((temp.y-pLoc.y)*(temp.y-pLoc.y)));                dist *= (1.0f/elapsedTime);                enemies[geid]->speed = dist;            }            grab = false; // no longer grabbing        }        enemies[geid]->setGrabLoc(pLoc);    } else {        for (int i=0;i<currNum;i++) {            if (enemies->location.x+20 > pLoc.x-24 && enemies->location.x-20 < pLoc.x+24) {                if (enemies->location.y+20 > pLoc.y-24 && enemies->location.y-20 < pLoc.y+24) {                    if (!Mouse::pLMB && Mouse::LMB) {                                                enemies->setGrabbed(true); // set inbound enemy as grabbed                            enemies->setGrabLoc(pLoc); // set to player loc                            grab = true; // set as grabbing                            geid = i; // store the index of curr grabbed enemy                            break;                    }                }            }        }    }


[Edited by - Ganoosh_ on September 28, 2006 7:44:44 PM]
I haven't looked too close to the code yet, but what happens if speed == nSpeed?
My Blog
I don't know how your comment here could be true (maybe you know that though?).
I don't think that this line is the cause, but you use lots of rands and if it's not working like you think, that might be the cause somewhere.
I gotta run now tho - good luck!
nSpeed = float(rand()%100+400); // random speed between 1000 and 10,000
Tadd- WarbleWare
Quote:Original post by kiome
I haven't looked too close to the code yet, but what happens if speed == nSpeed?


If speed == nSpeed, speed just stays the same unless altered by throwing the enemy.

Quote:Original post by reana1
I don't know how your comment here could be true (maybe you know that though?).
I don't think that this line is the cause, but you use lots of rands and if it's not working like you think, that might be the cause somewhere.
I gotta run now tho - good luck!
nSpeed = float(rand()%100+400); // random speed between 1000 and 10,000


lol, no it's not true. I had it between 1000 and 10,000 at first, then changed it to 400 - 500 because it was WAY too fast, and forgot to change the comment. Thanks for finding that though, that would've been confusing in the future. I fixed it.

This topic is closed to new replies.

Advertisement