platformer help

The Partridge Family were neither partridges nor a family. Discuss.
Waza
Posts: 5
Joined: July 13th, 2013, 12:56 pm

platformer help

Post by Waza » August 7th, 2013, 1:45 pm

Hey guys,
I'm on lesson 14 and started dicking around on how I would make a platformer. I encountered a problem with the loop detecting if there's a platform underneath the player. It works fine, but only works on the single platform. It's like the other ones don't even exist.

My guess is I made a stupid mistake that I just can't see.

Help me chili forums, you're my only hope.


Here's the code in question:

for(int index=0; index<nPlatform; index++)
{
if(PlayerY<=(PlatformYPos[index]-20) &&
(PlayerY+PlayerYSpeed)>=(PlatformYPos[index]-20) &&
(PlayerX+20)>=(PlatformXPos[index]) &&
(PlayerX)<=(PlatformXPos[index]+PlatformLenght[index]))
{
PlayerY=PlatformYPos[index]-20;
OnPlatform=true;
}
else if(PlayerY>=548)
{
PlayerY=548;
OnPlatform=true;
}
else
{
OnPlatform=false;
}
}
Attachments
123.zip
(35.69 KiB) Downloaded 132 times

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: platformer help

Post by cameron » August 7th, 2013, 2:24 pm

I see a few problems just looking at the code.
It should be:
if(PlayerY<=(PlatformYPos[index]+20) &&
(PlayerY)>=(PlatformYPos[index])&&
(PlayerX)>=(PlatformXPos[index]) &&
(PlayerX)<=(PlatformXPos[index]+PlatformLenght[index]))

I hope that works good luck.
Computer too slow? Consider running a VM on your toaster.

User avatar
Nosferatu
Posts: 36
Joined: July 25th, 2012, 7:08 am
Location: Slovakia

Re: platformer help

Post by Nosferatu » August 7th, 2013, 3:14 pm

Imagine that you have 2 platforms and player collides with the first one and the for loop have just started. Player collides with the first platform so OnPlatform is set to true. But then the loop continues and checks second platform which does not collide with player so it sets OnPlatform to false. So this way your collision works only with the last platform in the loop. Hope it helps

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: platformer help

Post by LuisR14 » August 7th, 2013, 7:30 pm

and in that case it would have to be like

Code: Select all

OnPlatform = OnPlatform && <current platform status>
(but depends on how you want to handle such onplatform status lol)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: platformer help

Post by SpaceAnimation » August 8th, 2013, 4:37 pm

Hey waza,
I'm not sure why you have a for loop? Does it test for every platform for every frame? Just seems like a waste of time. I haven't looked at the full code; I'm sure there is a reason for it.
My suggestion is changing it to a while loop with arguments for position of payer. This would be kind of a relationship or function member of player; is the surface at player's position solid. If true then players position is not opposed, else, players virticle axis position is altered in direction of fall.
Hmmm...do you get what I mean?
Spacey :geek:

Waza
Posts: 5
Joined: July 13th, 2013, 12:56 pm

Re: platformer help

Post by Waza » August 9th, 2013, 1:34 pm

thanks guys for all the replies, especially nosferatu who put his finger right on the booboo.

I put in a simple break so that as soon as one collision returned true the loop stops, and everything works perfectly now. Here's the updated code:

for(int index=0; index<nPlatform; index++)
{
if(PlayerY<=(PlatformYPos[index]-20) &&
(PlayerY+PlayerYSpeed)>=(PlatformYPos[index]-20) &&
(PlayerX+20)>=(PlatformXPos[index]) &&
(PlayerX)<=(PlatformXPos[index]+PlatformLenght[index]))
{
PlayerY=PlatformYPos[index]-20;
OnPlatform=true;
break;
}
else if(PlayerY>=548)
{
PlayerY=548;
OnPlatform=true;
}
else
{
OnPlatform=false;
}
}

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: platformer help

Post by SpaceAnimation » August 10th, 2013, 5:42 pm

Waza... Just a word on style. A for loop should always finish all of its loops. It's for this many do that; kind of scenario. A break in a for loop is not good style :(
I challenge you to change your loop into a while loop or do while loop.
I think that if you can do that then you would have greatly improved your understanding of programming and it's science. Just something I was taught. Hope u can like.
Spacey :geek:

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: platformer help

Post by LuX » August 10th, 2013, 9:26 pm

Yeah, your current code looks a bit weird, and what SpaceAnimation said might be helpful to learn, and if you have overlapping platforms it will take those in count as well.

For later stuff a break might boost the speed if you calculate a lot of stuff for every loop. But again, a few checks more won't affect your current method at all.
ʕ •ᴥ•ʔ

Waza
Posts: 5
Joined: July 13th, 2013, 12:56 pm

Re: platformer help

Post by Waza » August 19th, 2013, 4:38 am

Alright I hear ya.
another update:

{
int index=0;
OnPlatform=false;
while(index<nPlatform)
{
if(PlayerY<=(PlatformYPos[index]-20) && (PlayerY+PlayerYSpeed)>=(PlatformYPos[index]-20) && (PlayerX+20)>=(PlatformXPos[index]) && (PlayerX)<=(PlatformXPos[index]+PlatformLenght[index]))
{
PlayerY=PlatformYPos[index]-20;
OnPlatform=true;
}
else if(PlayerY>=548)
{
PlayerY=548;
OnPlatform=true;
}
index++;
}
index=0;
}

as for my code looking weird, can you elaborate on that Lux?
I'm aware that I have a lot to learn and any bad habits I might have that I can nip in the bud would be a big help.

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: platformer help

Post by SpaceAnimation » August 19th, 2013, 1:20 pm

Yea that looks better, but is still working exactly the same as previous code with 'for loop'. Ummm can you send me the full game with source code so i can understand why you have to check every platform for a collision. I would just stop checking when the player has a collision, as far as I know there is no reason to keep checking the rest of the platforms if you get a collision; the result you need to make sure your player is indeed not falling through space and has hit solid ground.The result you need to switch onPlatform = true.
Spacey :geek:

Post Reply