Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sprite "hanging" instead of following path


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 MrChrisnis   Members   -  Reputation: 340

Like
0Likes
Like

Posted 10 May 2012 - 02:30 PM

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

Sponsor:

#2 WavyVirus   Members   -  Reputation: 735

Like
0Likes
Like

Posted 10 May 2012 - 02:41 PM

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.

#3 MrChrisnis   Members   -  Reputation: 340

Like
0Likes
Like

Posted 10 May 2012 - 03:29 PM

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, 10 May 2012 - 03:38 PM.


#4 Servant of the Lord   Crossbones+   -  Reputation: 19592

Like
3Likes
Like

Posted 10 May 2012 - 03:43 PM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS