Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: on Fri 22/12/2006 05:53:33

Title: Mario-Type Collision Help
Post by: on Fri 22/12/2006 05:53:33
First, I've searched the forums and the manual and tried the Pixel Perfect Module (which, I honestly don't understand how to use)..

Okay, I'm just trying to do the basic Mario-type game.. jump on the bad guy's head, and he's dead... But everytime I do it, he either doesn't do anything.. or he dies as soon as I touch him, instead of when I jump on his head. I got the jumping and movement fine.. but once The Goomba touches Mario, or vice versa, the Goomba will die..

I've tried so many different combinations of coding..
I've used both IsCollidingWithChar and AreThingsOverlapping, but I think I just don't quite have an understanding of baselines, etc? But the manual doesn't really help me.

It's probably something easy that I'm doing wrong, but I just can't get it...

My room dimensions are 3807 x 252.

The Goomba sprite is 31x36
and the mario sprite is 37x48 if that helps...

I'm sorry if this is long and newbie-ish.. but I've been trying for about a week now, researching, trying new things, etc.. and to no avail.. thank you all in advance.
Title: Re: Mario-Type Collision Help
Post by: SSH on Fri 22/12/2006 08:30:37
How do you define "head"? If you just compare sprites, obviously its going to match whenever they touch. You need to be able to say where the head is, somehow. Probably the easiest thing to do is a rectangle overlap, but only of the upper N pixels of the "goomba" and the lower M pixels of Mario, then only the feet contacting the head will work. In fact, you only need check one line, probably.


int diffx=cGoomba.x - cMario.x;
int diffy=cGoomba.y - cMario.y;
if (diffx<0) diffx= 0-diffx;
if ((diffx < ((31+37)/2)) && (diffy > 30) && (diffy<36)) {
  // Kill the Goomba
}



Title: Re: Mario-Type Collision Help
Post by: Khris on Fri 22/12/2006 08:39:58
Shouldn't it be cGoomba.y-cMario.y? (The latter is the smaller one)
Title: Re: Mario-Type Collision Help
Post by: SSH on Fri 22/12/2006 08:50:31
I keep forgetting that the Y-coords in AGS are upside-down.
Title: Re: Mario-Type Collision Help
Post by: on Fri 22/12/2006 15:00:26
Sorry, for being stupid, lol.. But I'm still confused.. it should be simple.. the bottom of Mario's sprite touching ONLY the top of the goomba sprite..  but I still can't get it..

Was there something else I needed to add to that code (besides the action of the goomba dying)?

I thought about just making the goomba's baseline the top of his sprite and mario's 0.. so that mario's feet would have to touch the goomba's head.. but I can't seem to get the baselines right.

Gah.. I'm so confused.. It should be SOO simple and it probably is, lol

This is what I have so far.. and I'm using a platforming style of control (obviously):


if (character[GOOMBA].room ==4) {
 
if (character[GOOMBA].walking == 0) { // if  ghost has stopped
   MoveCharacterPath(GOOMBA,497,225);
   MoveCharacterPath(GOOMBA,350,225);
}

int diffx=cGoomba.x - cMario.x;
int diffy=cGoomba.y - cMario.y;
if (diffx<0) diffx= 0-diffx;
if ((diffx < ((31+37)/2)) && (diffy > 30) && (diffy<36)) {
cGoomba.StopMoving();
PlaySound(4);
ChangeCharacterView(GOOMBA,12);
Wait(10);
cGoomba.room=-1;} // Kill the Goomba
}
Title: Re: Mario-Type Collision Help
Post by: Khris on Fri 22/12/2006 16:33:52
1. The baselines don't matter collision wise, they only determine what's being drawn in front of each other. Lower baseline -> drawn in front.

2. The code should work, check out this pic:
(http://img294.imageshack.us/img294/872/explainmornk8.png)

The upper box is Mario, the lower one is the Goomba. The little red crosses are their pivots.
The green area represents the collision box set byif ((diffx < ((31+37)/2)) && (diffy > 30) && (diffy<36))

In my example, diffx=9 and diffy=34.

I'm not sure which platform algorithms you're using, but it is important that different x and y speeds are achieved by different time intervals, i.e. the sprite doesn't move more than 1 pixel in each direction between frames.

Otherwise it's possible that Mario's pivot will never touch that green box.

The important thing though is: What actually went wrong? I guess the Goomba didn't disappear, right?
Title: Re: Mario-Type Collision Help
Post by: on Fri 22/12/2006 17:00:14
Hmm.. I think I've figured out a quick way to do it..


if (character[GOOMBA].room ==4) {
 
if (character[GOOMBA].walking == 0) { // if  ghost has stopped
   MoveCharacterPath(GOOMBA,497,225);
   MoveCharacterPath(GOOMBA,350,225);
}
if (AreThingsOverlapping(EGO, GOOMBA)) {
 
  if (character[EGO].y == character[GOOMBA].y-14)
{
cGoomba.StopMoving();
PlaySound(4);
ChangeCharacterView(GOOMBA,12);
Wait(10);
cGoomba.room=-1;} // Kill the Goomba
}
}

...is there any reason I should have problems, bugs with that?

Also, thank you so much, everyone for the help!
Title: Re: Mario-Type Collision Help
Post by: on Fri 22/12/2006 17:37:10
Well that worked for jumping on them, but when I try to add code to have it hurt/kill the player when mario and the goomba touch, when he's NOT in the air, meaning when he's not jumping.. it gets all stupid, like i cant kill the goomba anymore..  can someone explain just HOW to implement this diffx, diffy thing..  I keep getting a "undefined symbol 'diffx'" message.. I'm so lost.. Sorry for being such a newbie... lol

So I either need to understand how to do that, or just help editing the code I've got to work, so that Mario gets hurt when they collide, but NOT when he's jumping on it.
Title: Re: Mario-Type Collision Help
Post by: on Fri 22/12/2006 18:57:40
Okay I think I got it.. It's rudimentary, but it works, I suppose:


if (character[GOOMBA].room ==4) {
 
if (character[GOOMBA].walking == 0) { // if  ghost has stopped
   MoveCharacterPath(GOOMBA,497,225);
   MoveCharacterPath(GOOMBA,350,225);
}
if (AreThingsOverlapping(EGO, GOOMBA)) {

if (character[EGO].y == character[GOOMBA].y-14) {
cGoomba.StopMoving();
PlaySound(4);
ChangeCharacterView(GOOMBA,12);
Wait(10);
cGoomba.room=-1;
}
}
if (character[GOOMBA].x-11 == character[EGO].x)
{ cGoomba.StopMoving();
  PlaySound(4);
}
else if (character[GOOMBA].x+11 == character[EGO].x){
cGoomba.StopMoving();
  PlaySound(4);
}
}
Title: Re: Mario-Type Collision Help
Post by: Khris on Fri 22/12/2006 19:19:00
Quote from: morbidsouldotcom on Fri 22/12/2006 17:37:10I keep getting a "undefined symbol 'diffx'" message..
Now THAT's what you should've told us from the start.

The only thing we need to nail it down now is the exact line that's throwing the error.

Btw, you are mixing old and new code, not a good idea. E.g. it should be "cGoomba.ChangeView(12);" instead of "ChangeCharacterView(GOOMBA, 12);" and cGoomba.AddWaypoint(...); instead of "MoveCharacterPath(GOOMBA, ...);"

Hurting Mario with your code is easy:
  if (AreThingsOverlapping(cEgo.ID, CGoomba.ID)) {
    int diffy=cGoomba.y-cMario.y;
   
    if (diffy==14) {
      // kill Goomba
    }
    else if (diffy==0) {
      // hurt Mario
    }
  }

Title: Re: Mario-Type Collision Help
Post by: on Fri 22/12/2006 19:49:23
i dont get any errors.. it just doesn't work..?

// script for Room: Repeatedly execute
if (character[GOOMBA].room ==4) {
 
if (character[GOOMBA].walking == 0) { // if  goomba has stopped
cGoomba.AddWaypoint(497,225);
    cGoomba.AddWaypoint(350,225);
}

if (AreThingsOverlapping(cEgo.ID, cGoomba.ID)) {
    int diffy=cGoomba.y-cMario.y;
   
    if (diffy==14) {
cGoomba.StopMoving();
PlaySound(4);
cGoomba.ChangeView(12);
Wait(10);
cGoomba.room=-1;      // kill Goomba
    }
    else if (diffy==0) {
PlaySound(5);      // hurt Mario
    }
  }
}



Maybe it has something to do with the platformer script I'm using? Gah, this is frustrating.. i understand it, but i dont know why it doesnt work,