# Gravity and collision

**URL:** <https://discourse.processing.org/t/gravity-and-collision/688>\
**Category:** Project Guidance\
**Tags:** homework\
**Created:** [June 3, 2018, 8:06pm UTC](https://discourse.processing.org/t/gravity-and-collision/688 "2018-06-03T20:06:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neojames](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@Neojames](https://discourse.processing.org/u/Neojames)\
**Post date:** [June 3, 2018, 8:06pm UTC](https://discourse.processing.org/t/gravity-and-collision/688/1 "2018-06-03T20:06:27Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Red\_Hen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/red_hen/32/323_2.png) [@Red\_Hen](https://discourse.processing.org/u/Red_Hen)\
**Post date:** [June 3, 2018, 8:17pm UTC](https://discourse.processing.org/t/gravity-and-collision/688/2 "2018-06-03T20:17:30Z")

</div>

Hello!

I thought you used very good English 🙂

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

---

<div class="post-metadata">

**Author:** ![Neojames](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@Neojames](https://discourse.processing.org/u/Neojames)\
**Post date:** [June 4, 2018, 6:40am UTC](https://discourse.processing.org/t/gravity-and-collision/688/3 "2018-06-04T06:40:54Z")

</div>

Thank you Red\_Hen, (Google translante is very usefull 😉

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);

---

<div class="post-metadata">

**Author:** ![Red\_Hen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/red_hen/32/323_2.png) [@Red\_Hen](https://discourse.processing.org/u/Red_Hen)\
**Post date:** [June 4, 2018, 7:46pm UTC](https://discourse.processing.org/t/gravity-and-collision/688/4 "2018-06-04T19:46:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neojames](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@Neojames](https://discourse.processing.org/u/Neojames)\
**Post date:** [June 5, 2018, 8:56pm UTC](https://discourse.processing.org/t/gravity-and-collision/688/5 "2018-06-05T20:56:16Z")

</div>

I’m unmasked, yes I am french 😇

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