#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Gravity with PGraphics

edited November 2014

I have a problem with creating gravity using a PGraphics as a floor. The PGraphics'll be drawn with the mouseX and mouseY position by clicking on the canvas.

I need that the ball walks over the drawn floor, and if there's no floor, it would fall down.

``````float x;
float y;
float gravity=.3;
float velocity = 0;
int caseM;
float counter;
PImage img;
boolean jump;

int xm= mouseX;
int ym  = mouseY;
int strokeW;
PGraphics pg;

void setup() {
x=20;
y=20;

strokeW = 25;
pg = createGraphics(1200, 700);

jump=false;
size(1200, 700);
}
void draw() {
pg.beginDraw();
pg.strokeWeight(strokeW);
if (mousePressed) {
pg.line(pmouseX, pmouseY, mouseX, mouseY);
}
pg.endDraw();
image(pg, 0, 0);

noStroke();
fill(255, 0, 0);
ellipseMode(CENTER);
ellipse(x, y, 30, 30);
ellipseMode(CORNER  );
obstacles();
jump();
}
void jump() {
if (jump==true) {
y-=5;
counter++;
velocity=0;
}
if (counter>20) {
jump=false;
counter=0;
}
}
void obstacles() {
color plat = pg.get ((int) (x), (int) y+20);
color plat1 = pg.get ((int) (x-20), (int) y);
color plat2 = pg.get ((int) (x+20), (int) y);
println(plat, plat1, plat2);
if (red(plat)!=0 && green(plat)!=0 && blue(plat)!= 0 && jump==false) {
y = y +velocity;
velocity = velocity + gravity;
}
else {
}
if (red(plat1)!=0 && green(plat1)!=0 && blue(plat1)!= 0) {
if (caseM==1) {
x-=5;
}
}
else {
}
if (red(plat2)!=0 && green(plat2)!=0 && blue(plat2)!= 0) {
if (caseM==2) {
x+=5;
}
}
else {
}
}
void keyPressed() {
switch(keyCode) {
case LEFT:
caseM = 1;
break;
case RIGHT:
caseM = 2;
break;

case UP:
jump= true;

break;
}
}

void keyReleased() {
caseM=0;
}
``````
Tagged: