Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SDL] Timer problem


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
5 replies to this topic

#1 DuckerDuck   Members   -  Reputation: 107

Like
0Likes
Like

Posted 20 June 2012 - 06:26 AM

Hello,
I'm currently trying to get familiar with SDL and C++, and i've been working on a little project involing a very basic game engine.
What I want to do at the moment is to implement a simple portal system. I have two portals, if my player steps on one of them he teleports to the other and vice versa. The problem is that, when using the portal, the collsion detection of the other portal interferese, and teleports the player infinitly from portal to portal. I wanted to fix this by adding a small delay before the portal is usable again, but somehow the function I wrote makes the game crash.
Here is the code i'm using:

To set the delay:
void Core::setPortalDelay(int time){
	Uint32 now;
	now = SDL_GetTicks();
  
while (now < time){
  PortalDelay = true;
}
if (now > time){
PortalDelay = false;
now = 0;
}
}

and to teleport the player:


if (CheckCol(PlayerLocation,rPortal1Loc) == true && PortalDelay == false){
  PlayerLocation.x = rPortal2Loc.x;
  PlayerLocation.y = rPortal2Loc.y;
  setPortalDelay(2000);
}
if (CheckCol(PlayerLocation,rPortal2Loc) == true && PortalDelay == false){
  PlayerLocation.x = rPortal1Loc.x;
  PlayerLocation.y = rPortal1Loc.y;
  setPortalDelay(2000);
}
Thanks,
DuckerDuck

Edited by DuckerDuck, 20 June 2012 - 06:27 AM.


Sponsor:

#2 ASnogarD   Members   -  Reputation: 212

Like
0Likes
Like

Posted 20 June 2012 - 06:54 AM

One of the issues is now = SDL_GetTicks();

This tells the game to get the total number of ticks since SDL was initialised, you want to find out the time that has passed since you used the portal.
To do that you need to work out the time the player used the portal , store that number away and then check each loop the current amount of time passing and subract from that amount the time the player used the portal.

something like :

only when the player uses the portal:
PortalUsedTime = SDL_GetTicks(); // note this must NOT get updated each loop, only when the player uses the portal

each loop:
CurrentTime = SDL_GetTicks(); // this gets updated each loop.
TimeSincePortalUsed = CurrentTime - PortalUsedTime;

Which will then make TimeSincePortalUsed give the number of milliseconds since the portal was used, which you can then compare to the desired delay time.


A alternative way to overcome the problem is to set a flag on the portals, so that when you use a portal the exit portal will set the flag to cannot use until it stops detecting a collision with the player, and then it sets the flag to be can be used.

Portal A - > B , player walks up to Portal A (flag set to can use) , Portal A sets Portal B flag to cannot use and teleports the player to Portal B, and while player is colliding with Portal B the flag remains cannot use but when the player walks out of collision range, Portal B sets its own flag to can use.

Hope that helps some.

#3 DuckerDuck   Members   -  Reputation: 107

Like
0Likes
Like

Posted 20 June 2012 - 08:43 AM

A alternative way to overcome the problem is to set a flag on the portals, so that when you use a portal the exit portal will set the flag to cannot use until it stops detecting a collision with the player, and then it sets the flag to be can be used.

Portal A - > B , player walks up to Portal A (flag set to can use) , Portal A sets Portal B flag to cannot use and teleports the player to Portal B, and while player is colliding with Portal B the flag remains cannot use but when the player walks out of collision range, Portal B sets its own flag to can use.


Thanks for the response, I tried to go with your idea with the flags, though I'm doing somwthing wrong here. Take a look:

if (CheckCol(PlayerLocation,rPortal1Loc) == true && PortalFlag1 == true){// if player collides AND the portal can be used, continue
 
  PortalFlag2 = false;  //set the destination portal to: cannot use
  PlayerLocation.x = rPortal2Loc.x;//teleport
  PlayerLocation.y = rPortal2Loc.y;//teleport
 
  while (CheckCol(PlayerLocation,rPortal2Loc) == true){  //while colliding with the portal
   PortalFlag2 == false;		 // keep it set to false
  }
   PortalFlag2 = true;		 // not colliding? set back to true
 
 
 

}
if (CheckCol(PlayerLocation,rPortal2Loc) == true && PortalFlag2 == true){ // if player collides AND the portal can be used, continue
 
  PortalFlag1 = false; //set the destination portal to: cannot use
  PlayerLocation.x = rPortal1Loc.x;//teleport
  PlayerLocation.y = rPortal1Loc.y;//teleport
  while (CheckCol(PlayerLocation,rPortal1Loc) == true){  //while colliding with the portal
   PortalFlag1 == false;		 // keep it set to false
  }
   PortalFlag1 = true;	    // not colliding? set back to true
 

 
}

To me it seems like it should work, but it crashes the game.

#4 ASnogarD   Members   -  Reputation: 212

Like
0Likes
Like

Posted 20 June 2012 - 08:58 AM

One thing I can see is :

while (CheckCol(PlayerLocation,rPortal2Loc) == true){ //while colliding with the portal
PortalFlag2 == false; // keep it set to false


should be PortalFlag2 = false; ( 1 = sign)

... same with the other portal.

#5 BeerNutts   Crossbones+   -  Reputation: 2946

Like
1Likes
Like

Posted 20 June 2012 - 08:59 AM

It looks like you'll have an infinite loop with this line:

while (CheckCol(PlayerLocation,rPortal2Loc) == true){ //while colliding with the portal
PortalFlag2 == false; // keep it set to false
}

That will never exit, because the player is obviously colliding with the portal, and you're not giving it a chance to exit the while loop

You should do something like this (pseudo code):
if (IsColliding(Player, Portal1) {
  if (PortalFlag1) {
    // PortalFlag1 says to make the portal move us to other portal
    Player.Pos = Portal2.Pos;
    PortalFlag2 = false;
  }
}
else if (!PortalFlag1) {
  // We've been transported to portal 1, and we've just stopped colliding with it. Set flag back to true
  PortalFlag1 = true;
}
// do same for Portal2

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#6 DuckerDuck   Members   -  Reputation: 107

Like
0Likes
Like

Posted 20 June 2012 - 10:20 AM

Thanks BeerNutts, you're pseudo code got the job done.




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