Gravity and collision

Hi everybody,

I need help with my first processing project.
I need to develop a mario like game for school, but I have some problems

How can I set a gravity ? Because when I trie to set y = y+1 in the test, the smiley is going down but when it is on the floor it is impossible to go on left or right, I have a problem with the test of the position but I don’t know where.

My second problem is that I don’t know how to set that when the smiley is near a specific tile it is necessary to tap a key (like ‘t’) to break the tile and if you don’t tap the key, the tile won’t break.

I hope that my explanation is not to bad because I am starting with developpement and there is a long time I have not write in English.

This is my actual sketch

import ptmx.*;

Ptmx map;
PImage smiley;
int x , y, score=0;
boolean left, right, up, down;

void setup() {
  size(800, 600);
  smiley = loadImage("smiley.png"); // charge une image smiley.png sous le nom smiley
  map = new Ptmx(this, "niv1.tmx");
  map.setDrawMode(CENTER);
  map.setPositionMode("CANVAS");//Default Position Mode
  x = int(map.mapToCanvas(map.getMapSize()).x / 2);
  y = int(map.mapToCanvas(map.getMapSize()).y / 2);
  imageMode(CENTER);
}

void draw(){
  background(map.getBackgroundColor());
  map.draw(x, y);
  image(smiley, width / 2, height / 2); //défini la position du smiley par rapport à la fenetre d'affichage
  textSize(24);
  fill(128);
  text("Score " +score, 300,18);
  int prevX = x;
  int prevY = y;
  if(left) x -= 3;
  if(right) x += 3;
    PVector overTile = map.canvasToMap(x, y+15);
    switch(map.getTileIndex(0, round(overTile.x), round(overTile.y))){
    case 4: 
      break;
    case 5: 
      map.setTileIndex(0, round(overTile.x), round(overTile.y), 4);
      score= score+1;
      break;
    default:
      x = prevX;
    }
  if(up) y -= 3;
  if(down) y += 3;
  switch(map.getTileIndex(0, round(overTile.x), round(overTile.y))){
    case 4: 
            y = y+1;
      break;
    case 5: 
      map.setTileIndex(0, round(overTile.x), round(overTile.y), 4);
      score= score+1;
              y = y+1;
      break;
    default:
      y = prevY;
  }
    }

void keyPressed(){
  if(keyCode == LEFT) left = true;
  if(keyCode == RIGHT) right = true;
  if(keyCode == UP) up = true;
  if(keyCode == DOWN) down = true;
}

void keyReleased(){
  if(keyCode == LEFT) left = false;
  if(keyCode == RIGHT) right = false;
  if(keyCode == UP) up = false;
  if(keyCode == DOWN) down = false;
}
1 Like

Hello!

I thought you used very good English :slight_smile:

Perhaps you could only switch on gravity when you are above a certain height on the y axis?

If this is too simple a solution, then the next step would be use a ‘grounded’ bool, which, if true, means that gravity should be OFF. If ‘grounded’ is false (i.e. your player is not touching any ground), then gravity should be ON.

Hope that helps, buddy! Good luck!

B

1 Like

Thank you Red_Hen, (Google translante is very usefull :wink:

I’am gonna try your second solution.

I have one more question, for the moment if you launch the sketch, the smiley is going down but I think the test is not good because when it arrive on a ground tile, it is blocked. I think that when the test is done, it is “too late”, the smiley is on the ground tile and not above the tile.

How can I do to test if the smiley is above the tile and not on the tile ? I think I don’t understand how to use correctly the PVector command

PVector overTile = map.canvasToMap(x, y);

Pas mal!

Alors, you could test for a collision if you have a position (in a vector, x,y) and a radius (float) of both your tile and your player.

You could then ask if these two objects are over-lapping, which would mean a collision.

First, subtract one position vector from the other.
Then, ask for the magnitude of the resultant vector. This magnitude is the distance between the player and the tile.
So, if this distance is less than the radius of the player plus the radius of the tile, then they must be over-lapping (colliding).

So, you just need to make sure you use a correct subtraction function, and then a magnitude retrieval function. You cannot just subtract vectors like you might subtract ordinary numbers.

Bon chance

B

1 Like

I’m unmasked, yes I am french :innocent:

Thanks for your help.I start to understand. Now I need to translate this in processing :grin:

1 Like