Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411491 Posts in 69377 Topics- by 58433 Members - Latest Member: graysonsolis

April 29, 2024, 10:03:36 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Handling collisions of objects w/o for-knowing their velocities.
Pages: [1]
Print
Author Topic: Handling collisions of objects w/o for-knowing their velocities.  (Read 897 times)
Gagege
Level 1
*



View Profile WWW
« on: March 01, 2010, 11:48:58 AM »

I have a character falling and accelerating. He's going to hit the ground eventually and he has to stop on it rather than fall through it(clearly). But, without knowing what his velocity is going to be when he hits the ground how do I get him to stop on it and not below it?

I'm pretty much a beginner at this stuff so the answer could be simple but it has escaped me so far.

Thanks in advance!
Logged

blog
Soli Deo Gloria
Martin 2BAM
Level 10
*****


@iam2bam


View Profile WWW
« Reply #1 on: March 01, 2010, 11:58:24 AM »

It's sound like you're not using an integration-aproach. Are you using the gravity equation?

The simplest way to detect the collision is through overlapping of shapes (The shape of your falling body against the shape of the ground).

Please elaborate a bit more so everybody can better understand what are you asking.

Logged

Working on HeliBrawl
Gagege
Level 1
*



View Profile WWW
« Reply #2 on: March 01, 2010, 12:10:45 PM »

Basically I'm adding 1 to the player's velocity for every frame that he falls. So, the farther he falls, the faster his Y position moves.
I'm kind of flying blind here

the variable "jumping" is initialized to true before anything else happens. Maybe a better name would be "inAir".
Code:
if( playerEvent.type == SDL_KEYDOWN )
    {
        switch( playerEvent.key.keysym.sym )
        {
            case SDLK_RIGHT: xVelocity += 8; break;
case SDLK_LEFT: xVelocity -= 8; break;
case SDLK_SPACE: jumping = true; jumpPower = 10; break;
        }
    }

I'm just trying to get him to stop at an invisible line for now (500px down from the top).

Code:
if(jumping)
{
if(jumpPower > 0)
{

fallPower = 0;
yVelocity -= jumpPower;
slowing++;
jumpPower -= slowing;
}
else
{
yVelocity = fallPower;
fallPower += 1;
}
}

if(yPosition >= 500)
{
yPosition -= yVelocity;
yVelocity = 0;
jumping = false;
slowing = 0;
}
else
{
yPosition += yVelocity;
}

xPosition += xVelocity;
Another problem is that it looks like he's bumping his head on an invisible ceiling when he jumps.
Logged

blog
Soli Deo Gloria
Gagege
Level 1
*



View Profile WWW
« Reply #3 on: March 01, 2010, 01:09:17 PM »

Ok, I figured out by myself how to fix these.
Thanks anyway.
Logged

blog
Soli Deo Gloria
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic