Collision and Boundaries help(Mario)?

Hi, I was working on a game and I had a couple questions. First, can you make a boundary by color? For example, I have a Mario and some pipes for him to jump over. The pipes are green so is there any code that prevents Mario from entering a green section but rather needing to go over(jump)?
Second questions was about collisions. I wanted to know can you make Mario “die” when touched by an obstacle such as goomba but when mario jumps over the goomba and lands directly on top it would cause the goomba image to disappear. I’m really sorry for the detailed questions, I’m trying to learn processing on my own before I take a class and its a bit difficult without a teacher.

Your mario is probably a sprite at some position (x,y), with a given width (w) and height (h), right? So imagine he is a rectangle. Similarly, obstacles that Mario can stand on (and not walk into) are also probably things that can be modeled as rectangles. Enemy sprites are also rectangles. Everything is a rectangle!

http://www.jeffreythompson.org/collision-detection/rect-rect.php

The procedure is thus this:

  1. Remember where Mario is currently.
  2. Try to move Mario.
  3. Check for collisions with all things.
  4. Is a collision happening now? If not, you’re done!
  5. Is this thing Mario is colliding with a wall? Then Mario can’t be at this new position, so reset his position to what you remembered it was in Step 1.
  6. Is this thing Mario is colliding with an enemy? Check to see if Mario is moving downward. If he is, kill the enemy. If not, kill Mario.
1 Like

Thanks again for helping! The procedure made things easier to understand but I’m having problems overall with the collision process and how to check with collisions with all things? I was wondering if you also know of another link/reference that could simplify the idea for me. Thanks once again

Well, I assume you have a way to track which walls/blocks/pipes and which enemies are on the screen. Since you are drawing them, you know where they are and how big they are.

It’s just a couple of loops to do these checks.

float pmX = marioX;
float pmY = marioY;
for( <every block> ){
  if( collides( marioX, marioY, marioW, marioH, blockX, blockY, blockW, blockH ) ){
  marioX = pmX;
  marioY = pmY;
}

for( <every foe> ){
  if( collides( marioX, marioY, marioW, marioH, foeX, foeY, foeW, foeH ) ){
  if( marioDY > 0 ){
    foe.kill();
  } else {
    mario.kill();
  }
}

Of course you’ll need the collides() function, which you can get from that link I posted, and you’ll need to work out your own loops and data structures. Are your Blocks/Enemies/Mario classes? That would be a good thing…

No, they’re not classes. How would that affect it though?

It would just make it a lot easier to write and maintain.

Hi again, I’ve been trying to get this too work but it seems like I still haven’t fully grasped the concept and am at a lost. Do you by any chance have another simple reference. I’m sorry I’m a beginner and trying to teach myself all of these but I’m slow I’m sure you’ve noticed

Post what code you have so far.

I deleted my code related to collisions because it wasn’t working at all and I wanted to work on some other aspects. Now that I think about it I should’ve commented it out…I know not posting code is going to make it harder to explain but I believe my code was something including
boolean name = hit(x,y,50,70,250,325,50,60);
It’s not much of a help but at this point I’d appreciate any help.

Well, I would start with the basics. You know you are going to have at least three types of objects. You will have a Mario. You will have Blocks. You will have enemies. Your sketch will also need setup() and draw() functions. So just knowing that, you have this code:

class Block {
  void draw() {
  }
}

class Foe {
  void draw() {
  }
}

class Mario {
  void draw() {
  }
}

ArrayList<Block> blocks = new ArrayList();
ArrayList<Foe> foes = new ArrayList();
ArrayList<Mario> marios = new ArrayList();


void setup() {
  size(600, 600);
}

void draw() {
  background(255);
  for (int i = 0; i < blocks.size(); blocks.get(i++).draw());
  for (int i = 0; i < foes.size(); foes.get(i++).draw());
  for (int i = 0; i < marios.size(); marios.get(i++).draw());
}

Copy, paste, and run this. Notice that it runs. There are no errors. It doesn’t DO anything, but it RUNS. Maintaining this property as you develop your code is important. After any change, make sure it still runs. If it doesn’t, you know exactly which code caused the problem.

Alright. What would you recommend I should add in first to prevent mistake?

Well, I started by filling in some details. We can have placeholder code to draw each of these things for a start. That means that each of these things is going to need a size and position - just so we can draw them! Then I added constructors for all the objects, and threw in some code to generate a few example objects:

class Block {
  float x,y,w,h;
  Block(float ix, float iy){
    x = ix;
    y = iy;
    w = 20;
    h = 20;
  }
  void draw() {
    fill(0,0,200);
    stroke(0);
    rect(x,y,w,h);
  }
}

class Foe {
  float x,y,w,h;
  Foe(){
    x = width/2;
    y = width/2;
    w = 20;
    h = 20;
  }
  void draw() {
    fill(0,200,0);
    stroke(0);
    ellipse(x+10,y+10,w,h);
  }
}

class Mario {
  float x,y,w,h;
  Mario(float ix, float iy){
    x = ix;
    y = iy;
    w = 20;
    h = 20;
  }
  void draw() {
    fill(200,0,0);
    stroke(0);
    ellipse(x+10,y+10,w,h);
  }
}

ArrayList<Block> blocks = new ArrayList();
ArrayList<Foe> foes = new ArrayList();
ArrayList<Mario> marios = new ArrayList();


void setup() {
  size(600, 600);
  for( int i = 0; i < 30; i++){
    blocks.add( new Block( 20*i, 580 ) );
    if( i > 10 && i < 15 ){
      blocks.add( new Block( 20*i, 480 ) );
    }
  }
  foes.add( new Foe() );
  marios.add( new Mario( 20, 560) );
}

void draw() {
  background(255);
  for (int i = 0; i < blocks.size(); blocks.get(i++).draw());
  for (int i = 0; i < foes.size(); foes.get(i++).draw());
  for (int i = 0; i < marios.size(); marios.get(i++).draw());
}

Now it is a scene, and you can start adding logic that deals with each of the types of things.

I’d start by making the enemies fall down if there are no blocks under them… Once that works, can you apply the same logic to Mario and Blocks?

Hmm I’ll continue trying to do that but so far no success. Haha hints would be appreciated. I probably won’t be able to work on it much Thanks for all the time you spent helping me.

I’m back but to no avail. I believe I learn better when there is a completed copy in front of me that i could look to for reference while figuring out myself what each part means. This is still very difficult for me.

Hi @tokyo – it is a bit concerning that, in all your posts to the forum, you have never shared any source code that you wrote. This really isn’t the best way for you to learn. I would recommend that you write small sketches where you try to get one thing working, then, when it is broken, you should share your complete small code example and the error message on this forum and ask how to accomplish something specific. This will get you a lot of great feedback.

Many people think that they learn best from studying complete examples, and that might be true for you, but it is often not the case. If you want to study complete examples of mario, there are plenty out there. Here are a few sketches and repositories with full source code:

https://www.google.com/search?q=mario+in+processing

There are also some tutorial walkthroughs! But you might learn a lot more, and more quickly, from breaking what you want to make down into smaller parts and working through them one at a time, asking for help as you go.