Jump to content
  • Advertisement
Sign in to follow this  
MrChrisnis

Sprite "hanging" instead of following path

This topic is 2264 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

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 :)

Share this post


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

Share this post


Link to post
Share on other sites

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. Edited by MrChrisnis

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!