Page 1 of 1

Solution to PhysicalResponse bugs.

PostPosted: Thu Feb 06, 2014 12:50 pm
by Hammerit
Solution to PhysicalResponse problems.
PhysicalResponse so easy to use so many problems to solve.
Or how to make almost perfect collision for platform game.
Problems:
1. Wall sticking - when Actor moves to the wall while jumping he will stick to it and stop falling down.
2. Glitching - when Actor hit the wall while walking on the ground he will do little jumps up and down.
3. Suicide moving - when near edge Actor will walk by himself until he fall.
4. Wall climbing - Actor will reach and climb platform even if he isn't suppose to.


Before solving this let me explain how PhysicalResponse works.
1. This event Activates when two Actors Collides.
2. PhysicalResponse calculates distance between two Actors. And put them in position where they will touch each other, changing X and Y coordinates .
But there is one problem. To calculate new position for our colliding Actors it uses ANGLES. But ANGLES always different from the ANGLE where it suppose to be. In simple word it will increase ANGLE by little and it changes position, each time when collision triggered.
Collision->PhysicalResponse->Calculates new position->Changes Angle->Changes x,y coordinates->Repositioning Actor
3. PhysicalResponse will set your colliding Actor XVELOCITY AND YVELOSIT to 0.


I should mention to move left or right i use velocity and my moving actor called HERO.

Solution:
1.Wall sticking:
Because PhysicalResponese after collision will set your YVELOSITY and XVELOSITY to 0, you should restore speed after PhysicalResponse happened.
Use this code in COLLISION LEFT OR RIGHT SIDE event.
Code: Select all
double TempYvelocity = hero.yvelocity;  //saving falling speed
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000, 1.000000, 0.000000, 0.000000);
// sets new coordinate X,Y for hero,
// sets Xvelocity,Yvelocity=0 for hero
if (HeroInTheAir == 1) // if hero is jumping or in the air, usually it is HEROCANJUMP == 0
{
   hero.yvelocity = TempYvelocity; // restore falling speed, prevents wall sticking
}

2.Glitching:
In this problem PhysicalResponse will change your Y coordinate because of that Actor glitching.
To solve this restore Y coordinate after PhyscailResponse.
Use this code in COLLISION LEFT OR RIGHT SIDE event.
Code: Select all
double TempYvelocity = hero.yvelocity; //saving falling speed
double TempYcoordinates = hero.y;      //saving Y coordinate of hero
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000, 1.000000, 0.000000, 0.000000);
// sets new coordinate X,Y for hero,
// sets Xvelocity,Yvelocity=0 for hero
hero.y = TempYcoordinates; // restore Y coordinate to prevent glitching and wall climbing
if (HeroInTheAir == 1)          // if hero is jumping or in the air, usually it is HEROCANJUMP == 0
{
   hero.yvelocity = TempYvelocity; // restore falling speed, prevents wall sticking
}

3.Suicide moving:
Because PhysicalResponse after collision will reposition your Actor changing both X and Y.
You Actor will slowly move to the end.
To solve this you can:
a) You can make yvelocity = 0 to stop collision => PhysicalResponse won't be trigert.
Because PhysicalResponse already makes Actors velocity = 0 when you collided top side, you need to restore it when you go out from ledge.
Use this code in DRAW ACTOR event.
Code: Select all
if (HeroInTheAir == 1 && hero.yvelocity < 5) // if hero is jumping or in the air and he's falling speed not greater then 5, usually it is HEROCANJUMP == 0
{
    hero.yvelocity += 0.5;
}
if (CollisionFree("hero",x,y+1) == 1)//checks if there is something under actor if not then hero is falling.
{
    HeroInTheAir = 1;
}

b) You can restore X coordinate after PhysicalResponse event.
If your hero constantly falling.
Use this code in COLLISION TOP SIDE event.
Code: Select all
if (hero.yvelocity > 0) //checks if hero falling down, it prevents blinking hero on top side of platform instead of going up.
{
    double TempXvelocity = hero.xvelocity; // saving xvelocity of hero
    double TempXcoordinates = hero.x;       // saving x coordinates of hero
    PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000, 1.000000, 0.000000, 0.000000);
    hero.x = TempXcoordinates;        //restore x coordinates
    hero.xvelocity = TempXvelocity;  //restore xvelocity speed
    HeroInTheAir = 0;
}


Code for colliding BOTTOM SIDE OF WALL. Pretty much the same except for HeroInTheAir = 0;
Code: Select all
if (hero.yvelocity < 0) //checks if hero still in jump, it prevents blinking hero on bottom side of platform instead of going down.
{
    double TempXvelocity = hero.xvelocity;
    double TempXcoordinates = hero.x;
    PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000, 1.000000, 0.000000, 0.000000);
    hero.x = TempXcoordinates;
    hero.xvelocity = TempXvelocity;
}

4.Wall climbing:
Because of constantly moving right to the wall PhysicalResponse after collision will reposition your Actor changing both X and Y coordinates.
In this problem PhysicalResponse will change your Y coordinate, so restore it after PhyscailResponse.
Use this code in COLLISION LEFT OR RIGHT SIDE event.
Code: Select all
double TempYvelocity = hero.yvelocity; //saving falling speed
double TempYcoordinates = hero.y;      //saving Y coordinate of hero
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000, 1.000000, 0.000000, 0.000000);
// sets new coordinate X,Y for hero,
// sets Xvelocity,Yvelocity=0 for hero
hero.y = TempYcoordinates; // restore Y coordinate of hero to prevent glitching and wall climbing
if (HeroInTheAir == 1)          // if hero is jumping or in the air, usually it is HEROCANJUMP == 0
{
   hero.yvelocity = TempYvelocity; // restore falling speed, prevents wall sticking
}



It will solve all bugs but:
1. It won't let you do "stair" walking any more. So use block type levels like first mario game.
2. It will glitch a little. If you fall while hugging wall, when you collide with top side of ground it will jiggle ones. (This i solved...)

Now you can do mario gap run.

And sorry for bad English it's not my native.

Re: Solution to PhysicalResponse bugs.

PostPosted: Thu Feb 06, 2014 8:55 pm
by Hares
3.Suicide moving:
Because PhysicalResponse after collision will reposition your Actor changing both X and Y.
You Actor will slowly move to the end.
To solve this you can:

Awsome, you fixed suicide moving 8)

Good tutorial. Thanks for sharing.

2. It will glitch a little. If you fall while hugging wall, when you collide with top side of ground it will jiggle ones. (This i solved...)

Can you also post the solution for this? Or it is something we should figure out for ourselves ... :wink: ?

Re: Solution to PhysicalResponse bugs.

PostPosted: Thu Feb 06, 2014 9:15 pm
by Hammerit
Hares wrote:
2. It will glitch a little. If you fall while hugging wall, when you collide with top side of ground it will jiggle ones. (This i solved...)

Can you also post the solution for this? Or it is something we should figure out for ourselves ... :wink: ?

To solve this:
1. Add global variable integer Hit.
2. In Collision Left or Side of ground add line.
Code: Select all
Hit = 1;

3. Add Collision Finished event with ground with code.
Code: Select all
Hit = 0;

4. Change KeyDown events to move left or right to:
For right.
Code: Select all
if (Hit  == 0)
{
 hero.xvelocity = 5;
}


It solves the problem, i'm not sure how. :D

Re: Solution to PhysicalResponse bugs.

PostPosted: Sat May 30, 2015 3:39 pm
by Zivouhr
Interesting info, thanks. Wall climbing in my first game Tomb of Twelve with the jump took some coding tricks to disable, though I'll keep this method in mind.

Re: Solution to PhysicalResponse bugs.

PostPosted: Sat Jun 25, 2016 7:56 pm
by nightFall16
Awesome tut man thanks it helped me so much

Re: Solution to PhysicalResponse bugs.

PostPosted: Sat Feb 09, 2019 5:13 am
by TDM
Thank you so much mate :D

Re: Solution to PhysicalResponse bugs.

PostPosted: Sun Apr 07, 2019 6:50 am
by purplecloud
Thank you for the awesome explanation! It helps me a lot. :)