Be a Supporter!
darrenfindlay
darrenfindlay
  • Member since: Jul. 10, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Platform Game 2009-07-10 13:58:58 Reply

Hi, I've started making a platform game and i'm having a bit of bother with collision detection when it comes the the player landing on the platform. heres the code

private function touching(e:Event)
{
if (Level.hitTestPoint(hero.x, hero.y, true) == true)
{
//trace("touching");
hero.vel = 0;
hero.y -= 1;
hero.Jump = false;
}
}

the problem is when the player is falling fast, he falls through the platform because he is falling faster than the height of the platform so the hitTestPoint co-ordinate never touches the platform.

Just wondering if anyone knows a way to solve this.

heres a link to my swf http://www.darrenfindlayportfolio.co.uk/
index2.html

Thanks in advance

darrenfindlay
darrenfindlay
  • Member since: Jul. 10, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Platform Game 2009-07-10 14:01:18 Reply

**Code is in AS3

Neo-13
Neo-13
  • Member since: Jun. 9, 2007
  • Offline.
Forum Stats
Member
Level 23
Programmer
Response to Platform Game 2009-07-10 14:17:12 Reply

Make the platform bigger or the framerate higher...? Anyway, after your hitTestPoint, why has you done ... == true? It is uneccessary.


BBS Signature
Neo-13
Neo-13
  • Member since: Jun. 9, 2007
  • Offline.
Forum Stats
Member
Level 23
Programmer
Response to Platform Game 2009-07-10 14:18:22 Reply

Oh yeah, use a while loop instead. That way you don't have to rely on the framerate, which may be too small if they are falling fast.


BBS Signature
darrenfindlay
darrenfindlay
  • Member since: Jul. 10, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Platform Game 2009-07-10 14:26:00 Reply

At 7/10/09 02:17 PM, Neo-13 wrote: Make the platform bigger or the framerate higher...? Anyway, after your hitTestPoint, why has you done ... == true? It is uneccessary.

yeah i thought about just making the platforms bigger butif the character falls from a great height he's still going to sink into the platform and then slowly move up. I dont think that increasing the framerate would help because the pixels per frame hes falling will still increase and he'l still fall into the platform.

Thanks for your reply though

Neo-13
Neo-13
  • Member since: Jun. 9, 2007
  • Offline.
Forum Stats
Member
Level 23
Programmer
Response to Platform Game 2009-07-10 14:27:34 Reply

That's why I changed my reply. See my second post.


BBS Signature
headphenomenon
headphenomenon
  • Member since: May. 1, 2009
  • Offline.
Forum Stats
Member
Level 13
Programmer
Response to Platform Game 2009-07-10 14:56:13 Reply

would you like for anybody to help you with this game? I can. I'm looking for someone who can get help with their flash. (No I will not ask for cash, so you don't have to dig through that dump you call a pocket for your wallet).


06-17-11: User of the Day
07-25-11: 1500th Post
08-09-11: Level 13!

darrenfindlay
darrenfindlay
  • Member since: Jul. 10, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Platform Game 2009-07-10 15:12:12 Reply

At 7/10/09 02:56 PM, U-Gamer wrote: would you like for anybody to help you with this game? I can. I'm looking for someone who can get help with their flash. (No I will not ask for cash, so you don't have to dig through that dump you call a pocket for your wallet).

Yeah that would be great!!

I got it working now btw, just added a few extra collision detections

if (Level.hitTestPoint(hero.x, hero.y, true))
{
//trace("touching");
hero.vel = 0;
hero.y -= 1;
hero.Jump = false;
}
if (Level.hitTestPoint(hero.x, (hero.y - 1), true))
{
//trace("touching");
hero.vel = 0;
hero.y -= 1;
hero.Jump = false;
}
if (Level.hitTestPoint(hero.x, (hero.y - 2), true))
{
//trace("touching");
hero.vel = 0;
hero.y -= 1;
hero.Jump = false;
}
if (Level.hitTestPoint(hero.x, (hero.y - 3), true))
{
//trace("touching");
hero.vel = 0;
hero.y -= 1;
hero.Jump = false;
}
if (Level.hitTestPoint(hero.x, (hero.y - 4), true))
{
//trace("touching");
hero.vel = 0;
hero.y -= 1;
hero.Jump = false;
}
if (Level.hitTestPoint(hero.x, (hero.y - 5), true))
{
//trace("touching");
hero.vel = 0;
hero.y -= 2;
hero.Jump = false;
}

darrenfindlay
darrenfindlay
  • Member since: Jul. 10, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Platform Game 2009-07-10 15:18:12 Reply

Thanks for your posts neo and U-gamer

Coolio-Niato
Coolio-Niato
  • Member since: Jun. 30, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to Platform Game 2009-07-10 18:45:20 Reply

At 7/10/09 03:12 PM, darrenfindlay wrote:
I got it working now btw, just added a few extra collision detections

That method you used is called multisampling, but sadly you did it inefficiently. (copy pasting chunks of code with just one number changing)

So try this.

If your y velocity is in a variable called yspd:

var checks =10 // this is the amount of checks you want to do between your initial and final position
var sample=yspd/checks // this is an increment of yspd by which you'll check the collision
var hit = false; // a boolean variable (true / false ) tracking if there was a collision
for(var i =0;i<checks;i++){ // loop from 0 to 9 times
     if(ground.hitTestPoint(char.x,char.y+sample*i,true){
         hit=true; // assign hit true because there was a collision
         break; // exit the loop cause you already found a collision
     }
}
if(hit==true) { // or rather just if (hit){  same thing
//DEAL WITH COLLISION
} else { // otherwise
//no collision, move char down the full yspd
}

That'll severly shorten your code :) Hope it helps.

darrenfindlay
darrenfindlay
  • Member since: Jul. 10, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Platform Game 2009-07-10 19:18:27 Reply

Thanks coolio!!

your post made a lot of differance to my game!

if(hero.vel > 0)
{
var checks = 20;

for(var i = 0; i < checks; i++)
{
if (Level.hitTestPoint(hero.x, hero.y - i, true))
{
//trace("touching");
hero.vel = 0;
hero.y -= i;
hero.Jump = false;
}
}
}

I changed the hero.y - 1 to hero.y - i and it stopped the character slowly floating up to the same level as the platform, now it looks much better !

plus my code is 10 times cleaner :)

thanks alot mate :)

starownage3
starownage3
  • Member since: Apr. 22, 2008
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Platform Game 2009-07-11 01:03:41 Reply

games should have 30 fps so that doesnt happen. Increase fps and shrink numbers. also put a cap at the momentum around the thickness of the thinest platform.


My friend's sister plays wizard 101....
she's 17.....
wtf?

starownage3
starownage3
  • Member since: Apr. 22, 2008
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Platform Game 2009-07-11 01:07:17 Reply

At 7/10/09 02:26 PM, darrenfindlay wrote:
At 7/10/09 02:17 PM, Neo-13 wrote: Make the platform bigger or the framerate higher...? Anyway, after your hitTestPoint, why has you done ... == true? It is uneccessary.
yeah i thought about just making the platforms bigger butif the character falls from a great height he's still going to sink into the platform and then slowly move up. I dont think that increasing the framerate would help because the pixels per frame hes falling will still increase and he'l still fall into the platform.

Thanks for your reply though

I made a script to fix that problem. i dont feel like typing it right now. im realy tired. i dont remember it very well. basicly make more hit tests slightly higher up in the character and make it increase its height if it touches the ground. It still might sink into the ground for one frame though


My friend's sister plays wizard 101....
she's 17.....
wtf?

Coolio-Niato
Coolio-Niato
  • Member since: Jun. 30, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to Platform Game 2009-07-11 11:34:32 Reply

At 7/10/09 07:18 PM, darrenfindlay wrote: Thanks coolio!!

your post made a lot of differance to my game!

Yep your way works too. :) Glad to help.