Page 2 of 5

Re: Perfect collision exemple

PostPosted: Wed Aug 24, 2011 8:39 pm
by Hblade
Lol I dunno :)

Re: Perfect collision exemple

PostPosted: Wed Aug 24, 2011 9:47 pm
by lcl
Game A Gogo wrote:how did you make that happen? (Btw the mario sprite has nothing do to with collision, it's on another hidden actor, so his nose got nothing to do with it o: )

EDIT: just found out how... pretty strange, I think it's because it's not detecting anything under mario, so the yvelocity=0; happens later, or something S:

No, I dragged the collision actor to that point and actually, at that exact point one pixel of that collision actor is on top of the ground causing Mario to stand in air :D

Re: Perfect collision exemple

PostPosted: Wed Aug 24, 2011 10:08 pm
by Hblade
Yeah D:

Re: Perfect collision exemple

PostPosted: Thu Aug 25, 2011 8:45 pm
by Game A Gogo
lcl wrote:
Game A Gogo wrote:how did you make that happen? (Btw the mario sprite has nothing do to with collision, it's on another hidden actor, so his nose got nothing to do with it o: )

EDIT: just found out how... pretty strange, I think it's because it's not detecting anything under mario, so the yvelocity=0; happens later, or something S:

No, I dragged the collision actor to that point and actually, at that exact point one pixel of that collision actor is on top of the ground causing Mario to stand in air :D


Ah then it's my collide mask :D thanks for pointing it out! means I better find a better collide shape

Re: Perfect collision exemple

PostPosted: Fri Aug 26, 2011 1:19 am
by Jagmaster
This is a good example. I like the fact that the actor can easily go over curves. :D When I added a new actor though, Mario wanted to "Collision Free" with my new actor. Is there some way to get around this?

Edit: I just realized that one can just use CollisionState to disable a collision.

Re: Perfect collision exemple

PostPosted: Sat Apr 28, 2012 9:48 pm
by lverona
Hey fellas!

I've been studying this "perfect collision" method for a while and decided to post commented code. If Game A Gogo notices some error in my understanding of the code, I would be glad to fix my comments.

Meanwhile, here it is:

Code: Select all
float MSensor=1.2;
char*key=GetKeyState();

//xvelocity becomes either 2 or -2 depending on the key pressed
//if both or none are pressed it is 0
xvelocity=(key[KEY_RIGHT]-key[KEY_LEFT])*2;

//if ceiling is high enough
//but if we land too deep into ground, lift player up 2 pixels
if(abs(xvelocity)>0&&CollisionFree("Event Actor",x+xvelocity,y-5)&&
CollisionFree("Event Actor",x+xvelocity,y-1)==0)y-=2;
//if ceiling is too low, we cannot move
else if(CollisionFree("Event Actor",x+xvelocity,y-5)==0)xvelocity=0;

//determine the direction of movement
//if "1" means key right is pressed, if "-1" key left
if(abs(xvelocity)>0)PlayerWD=xvelocity/abs(xvelocity);

//if xvelocity is larger-equal than movement sensor (we definitely move)
//and if yvelocity is smaller-equal to movement sensor (we are not falling too fast)
//we draw going right animation
if(xvelocity>=MSensor&&abs(yvelocity)<=MSensor)ChangeAnimation("actor", "goright1", NO_CHANGE);
//or if xvelocity is smaller-equal than negative movement sensor (we move another direction)
//and we are not falling too fast
//we draw going left animation
else if(xvelocity<=-MSensor&&abs(yvelocity)<=MSensor)ChangeAnimation("actor", "goleft1", NO_CHANGE);

//if we are not moving and not falling, we draw stand animation
if(abs(xvelocity)<MSensor&&abs(yvelocity)<=MSensor)
{
    //here we choose which direction to draw
    if(PlayerWD==1)ChangeAnimation("actor", "standright", NO_CHANGE);
    else ChangeAnimation("actor", "standleft", NO_CHANGE);
}

//if we are definitely falling
//we draw falling animation
if(yvelocity>=MSensor)
{
    if(PlayerWD==1)ChangeAnimation("actor", "standright", NO_CHANGE);
    else ChangeAnimation("actor", "standleft", NO_CHANGE);
}

//if we are "falling" the other direction, means we are jumping
else if(yvelocity<=-MSensor)
{
    if(Jump==1)PlaySound2("data/BUTTON01.WAV", 0.233333, 1, 0.000000);
    if(PlayerWD==1)ChangeAnimation("actor", "jumpright", NO_CHANGE);
    else ChangeAnimation("actor", "jumpleft", NO_CHANGE);
}

//if there is no collision immediately below us
//and no collision even 1 pixel more below us
//we turn gravity on and disallow jumping
if(CollisionFree("Event Actor",x,y+yvelocity)||
CollisionFree("Event Actor",x,y+yvelocity+1)){
yvelocity+=0.16;Jump=0;
}
//but if there is a collision, we turn gravity off
else {
//we turn off ability to jump if there is a
//collision near top of player;
if(CollisionFree("Event Actor",x,y+yvelocity-4)==0)Jump=0;
//if nothing is near top of player, we allow jumping
else {
Jump=1;
}
     }
yvelocity=0;
}

yvelocity-=key['z']*Jump*jump_height;


In that code I would question the need for PlayerWD when you could just check if xvelocity>0 to determine the direction. However, perhaps having a global player direction variable could prove useful later on.

Re: Perfect collision exemple

PostPosted: Sun Apr 29, 2012 4:31 am
by Game A Gogo
PlayerWD holds the last direction the played went into, because if player is not moving, xvelocity is not >0 or <0, it's ==0 :)

At least that's what I think PlayerWD is xD I can't remember off the top of my hat what the variable is exactly for, but that's what I presume :)

Re: Perfect collision exemple

PostPosted: Mon Apr 30, 2012 11:31 pm
by happyjustbecause
I really want to use this method for my collisions in Night Knight. I think it's great how you can't just run into walls and look buggy. It'd also be great to easily go over curves smoothly. I need to study this and implement it!!! So, expect some questions in the near future, hopefully you or someone will be willing to answer.

Re: Perfect collision exemple

PostPosted: Tue May 01, 2012 7:46 pm
by lverona
happyjustbecause, I am now using this method and so far I think it is very good. I do encourage everyone to use this method, at least to handle collisions of the player. Reasoning is simple - in a typical platform game most objects the player encounters are things that he cannot walk into, which need to be "firm". Physical Response method goes from the premise - "let's start with the situation that a player by default can walk through any object". CollisionFree approach has a different premise: "let's start with a situation where a player by default cannot walk through any object". Clearly, the latter makes more sense for a player actor.

Re: Perfect collision exemple

PostPosted: Wed May 02, 2012 6:37 am
by happyjustbecause
Well, I want to use this method for collision, but I'm not sure what code I need to take from this, because all I want is collision related code, but the draw actor of the collider has gravity, key down events, and changing animations in it. Is it required to have that code in it? Is there any way you (someone up to it) can assist me in getting just the collision method in this, or is it all collision related? Sorry I'm so confused about this, I've just never really dealt with such perplexing, at least perplexing looking code before.

Re: Perfect collision exemple

PostPosted: Wed May 02, 2012 8:13 am
by phyzix5761
I would also like to see a variation of this code just for the collision and not for gravity.

Re: Perfect collision exemple

PostPosted: Wed May 02, 2012 8:43 am
by lverona
Well, in this post: viewtopic.php?f=6&t=9992&p=84447#p84257 I tried to comment the code so that it is easier to understand.

If you don't want gravity, just exclude this part from the code: yvelocity+=0.16;

You can also not include the key down stuff, I don't see an immediate problem.

Re: Perfect collision exemple

PostPosted: Wed May 02, 2012 9:40 am
by lverona
I am now thinking over - how to work moving platforms? I haven't tried horizontal platforms, but with vertically moving platform there is a following problem: when the platform is moving DOWN and you are jumping on it from top, then the player never lands on the platform, but keeps touching it, then being pushed back, then falling and touching it again, then pushed back. The only way when the player can actually land on the vertically moving platform is when it moves UP.

This basically brings up the need for a certain function that would recognise the collision with an object, but not act as an actual collision. Meaning that one can have areas defined that would react to the player touching them, but at the same time being CollisionState disabled.

If there is such a function, I would really like to know, since it is crucial for a CollisionFree approach, otherwise it becomes very difficult to make the player interact with the environment other than bumping into it.

Here's an example: I want to pass a wire frame and by that action trigger a sound.
With a PhysicalReponse approach I can do this by just registering a collision. Not issuing a Physical Reponse I play a sound.
With a CollisionFree approach I cannot really do this. If I set wire frame to CollisionState=DISABLE, then it simply ignores any collision event at all.

So this is the fundamental problem here which needs a solution ;)

Re: Perfect collision exemple

PostPosted: Wed May 02, 2012 5:07 pm
by skydereign
I mentioned this problem in one of the many topics about this method that have popped up. There isn't such a function, but I'll try to see if I can make a CollisionFree2 function that can be used to workaround this. There is one thing that could work (but it isn't very dynamic). If you use filled regions, you can use getactor, to get the region (only works when the filled region is the top actor).

Re: Perfect collision exemple

PostPosted: Wed May 02, 2012 6:44 pm
by lverona
How do you envision it should work, this CollisionFree2?