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;
}
}
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.
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.
It looks like you'll have an infinite loop with this line:
[color=#000088]while[color=#000000] [color=#666600]([color=#660066]CheckCol[color=#666600]([color=#660066]PlayerLocation[color=#666600],[color=#000000]rPortal2Loc[color=#666600])[color=#000000] [color=#666600]==[color=#000000] [color=#000088]true[color=#666600]){[color=#000000] [color=#880000]//while colliding with the portal
[color=#000000] [color=#660066]PortalFlag2[color=#000000] [color=#666600]==[color=#000000] [color=#000088]false[color=#666600];[color=#000000] [color=#880000]// keep it set to false
[color=#000000] [color=#666600]}
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