Perfect collision exemple

Post here your demos and examples with the source files.
Forum rules
Always post the games with a screenshot.
The file must have the ged and data files (complete game source)
Use the forum attachment to post the files.
It is always better to use the first post to put the game files

Re: Perfect collision exemple

Postby Hblade » Wed Aug 24, 2011 8:39 pm

Lol I dunno :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Perfect collision exemple

Postby lcl » Wed Aug 24, 2011 9:47 pm

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
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Perfect collision exemple

Postby Hblade » Wed Aug 24, 2011 10:08 pm

Yeah D:
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Perfect collision exemple

Postby Game A Gogo » Thu Aug 25, 2011 8:45 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Perfect collision exemple

Postby Jagmaster » Fri Aug 26, 2011 1:19 am

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.
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: Perfect collision exemple

Postby lverona » Sat Apr 28, 2012 9:48 pm

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.
Last edited by lverona on Wed May 02, 2012 6:56 pm, edited 2 times in total.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Perfect collision exemple

Postby Game A Gogo » Sun Apr 29, 2012 4:31 am

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 :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Perfect collision exemple

Postby happyjustbecause » Mon Apr 30, 2012 11:31 pm

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.
For small creatures such as we the vastness is bearable only through love.
-Carl Sagan

Night Knight Development Thread
User avatar
happyjustbecause
 
Posts: 267
Joined: Tue Jul 26, 2011 3:10 pm
Location: Frazier Park, Ca
Score: 15 Give a positive score

Re: Perfect collision exemple

Postby lverona » Tue May 01, 2012 7:46 pm

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.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Perfect collision exemple

Postby happyjustbecause » Wed May 02, 2012 6:37 am

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.
For small creatures such as we the vastness is bearable only through love.
-Carl Sagan

Night Knight Development Thread
User avatar
happyjustbecause
 
Posts: 267
Joined: Tue Jul 26, 2011 3:10 pm
Location: Frazier Park, Ca
Score: 15 Give a positive score

Re: Perfect collision exemple

Postby phyzix5761 » Wed May 02, 2012 8:13 am

I would also like to see a variation of this code just for the collision and not for gravity.
phyzix5761
 
Posts: 261
Joined: Sun Feb 27, 2011 4:28 am
Score: 18 Give a positive score

Re: Perfect collision exemple

Postby lverona » Wed May 02, 2012 8:43 am

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.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Perfect collision exemple

Postby lverona » Wed May 02, 2012 9:40 am

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 ;)
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Perfect collision exemple

Postby skydereign » Wed May 02, 2012 5:07 pm

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).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Perfect collision exemple

Postby lverona » Wed May 02, 2012 6:44 pm

How do you envision it should work, this CollisionFree2?
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

PreviousNext

Return to Game Demos

Who is online

Users browsing this forum: No registered users and 1 guest

cron