Skip to main content
gamedev.net
🔒 Locked

Sprite "hanging" instead of following path

Started by MrChrisnis 2 replies 1.7k views
Original Post
MrChrisnis
MrChrisnis
I have a projectile motion program made in C++ using SDL (Simple DirectMedia Layer) that fully works in regards to the projectile moving in an arc according to gravity and all that. My problem is that when the projectile starts to move, it will move a certain number of frames, and then hang for a while, and then jump forward and repeat the process. It is like the sprite is kind of lagging for a bit.

I haven't posted any code yet because I was wondering whether this might just be a common error that happens but if code is needed then I shall post it

Thanks for any help people give

Chris
WavyVirus
WavyVirus
I think more details are probably required to answer your question. How are you handling the amount of time between each frame and deciding how far to move the projectile? This is one area of the code which might be the culprit.

You should try stepping through your program in a debugger or printing out some values each frame to make sure things are working as you expect.

If you're still stuck then try creating a copy of your code and trimming it down as much as possible while still leaving enough to demonstrate the problem - then you can post it here for some help.
MrChrisnis
MrChrisnis

void Projectile::setVelocities(float speed, float angleInDeg)
{
set_velocities(speed*cos(angleInDeg*PI/180), speed*sin(angleInDeg*PI/180));
}
void Projectile::updatePosition(float timeInSecs){
set_world_position_x(initialX + get_x_velocity() * timeInSecs);
set_world_position_y(initialY - get_y_velocity() * timeInSecs + 0.5 * g * timeInSecs * timeInSecs);
}
//Above is the Projectile Functions
//Below is the stuff in my game class file relating to the projectile (apart from the constructor, but that works fine)
projectileMove = false;

SDL_Event event;
bool gameover = false;
while (!gameover){
SDL_Delay(10);
if(SDL_PollEvent(&event)){
switch (event.type){
case SDL_KEYDOWN: //handle the keyboard
switch(event.key.keysym.sym){
case SDLK_SPACE:
projectileTime->reset();
proj->setVelocities(70, 40);
projectileMove = true;
break;
case SDLK_ESCAPE:
gameover = 1;
break;
}
break;
case SDL_QUIT:
gameover = true;
break;
}
if(proj->get_x() >= 630 || proj->get_y() > 350)
{
delete proj;
proj = NULL;
proj = new Projectile("Pokeballs.bmp", 10, 350);
proj->setVelocities(0, 0);
projectileMove = false;
}
if(projectileMove)
{
proj->updatePosition(projectileTime->getTimeInSecs());
}
}


EDIT:

I just found out that the movement is fine as long as the mouse is moving around on the screen, or something else is happening with the computer (say a key is pressed). I came up against this problem before but I don't think I managed to fix it.
Servant of the Lord
Servant of the Lord
Your problem is, you have your projectile movement code within your event loop (the "if(SDL_PollEvent(&event))" part). It needs to be outside that function, otherwise your projectile will only update when the window is getting input (like the mouse moving or something), this explains the seemingly sporadic behavior.

Just move the update code to be after the if(SDL_PollEvent()) statement, and not inside of it.

You have two other problems that aren't specifically causing you problems, but may come back and bite you later (unrelated to the problem that you were requesting help for).
1) You have a switch() inside another switch(). This is fine... BUT! you need to keep in mind that a 'break' statement will only break out of the first switch()/while()/for() statement it is in, and not any further encompassing ones. (This is a 'potential' problem, not a current problem).

2) You are using "if(SDL_PollEvent(&event))", when you should use "while(SDL_PollEvent(&event))". Otherwise, you only ever process one event a frame, but it is very easy and a common thing for your program to recieve more than one event a frame, leading to a backlog of events that it needs to deal with that keep getting pushed to the next frame, and could cause a delay between, for example, moving the mouse and the program actually handling it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.