#### Howdy, Stranger!

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

# Jumping??

edited February 2018

Hi, how can I make an object jump??

Here is what I have made (I want the box to only jump once):

``````float playerX = 200;
float playerY = 900;
float gravity = 10;
boolean override = true;

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

void player() {
fill(255, 0, 0);
rect(playerX, playerY, 30, 30);
}
void draw() {
background(255, 255, 255);
player();

if (override == false) {
constrain(playerY, 0, 1000);
}
if (keyPressed == true) {
playerY -= 20;
override = true;
}
if (playerY < 900 && keyPressed == false) {
playerY += gravity;
}
}
``````
Tagged:

• edited February 2018 Answer ✓
``````float px,py,vx,vy,ax,ay;
boolean can_jump = false;

void setup() {
size(600,400);
ax = 0;
ay = .32;
vx = 0;
vy = 0;
px = 300;
py = 200;
}

void player() {
fill(255, 0, 0);
rect(px-15, py-30, 30, 30);
}

void draw() {
background(255, 255, 255);
vx+=ax;
vy+=ay;
px+=vx;
py+=vy;
if( py > height ){
py = height;
vy = 0;
can_jump = true;
}
player();
}

void keyPressed(){
if( can_jump ) {
vy = -10;
can_jump = false;
}
}
``````
• edited February 2018

Thanks a lot guys!! I was also wondering how I could input this into this code (This is only a snippet):

``````case "level6"  :
{
background(135, 206, 235);
drawSea();
if (keyCode == DOWN && keyPressed == true) {
imgY += anim;
}
if (keyCode == UP && keyPressed == true) {
imgY -= anim;
}

useCloud();
cloud1 -= 2;
cloud2 -= 2;
cloud3 -= 2;
cloud4 -= 2;
cloud5 -= 2;
fill(0, 0, 0);
text("Survive for 100 seconds!", 830, 50);
text(str(timer.time()/1000) + " seconds", 920, 100);
if (timer.time()/1000 >= 80) {
scene = "win";
lock6 = true;
}

for (int i = 0; i < e.length; i++) {
e[i].eagle();
e[i].move();
}
for (int i = 0; i < f.length; i++) {
f[i].fishHook();
f[i].move();
}
for (int i = 0; i < e.length; i++) {
if (dist(imgX, imgY, e[i].x - 33, e[i].y + 88) < 30) {
scene = "lose";
lose = " were eaten by an eagle!";
loseImage = pic10;
}
}
for (int i = 0; i < f.length; i++) {
if (dist(imgX, imgY, f[i].x - 161, f[i].y + 117) < 30) {
scene = "lose";
lose = " were caught by a fisherman!";
loseImage = pic23;
}
}
break;
}
``````
• Shameless self-promotion: I wrote a tutorial on collision detection, including jumping, available here: http://happycoding.io/tutorials/processing/collision-detection

• Awesome!!! I still ran into the problem of the fish(character of the game). This is the part I am having trouble:

``````  case "level6"  :
{
background(135, 206, 235);
drawSea();
if (keyCode == DOWN && keyPressed == true) {
imgY2 += anim;
}
if (keyCode == UP && keyPressed == true) {
imgY2 -= anim;
}

image(pic16, imgX2, imgY2, imgW, imgH);

if (keyPressed == true && keyCode == CONTROL) {
override = true;
if (imgY2 + imgH > seaY) {

//snap the player's bottom to the ground's position
imgY2 = seaY - imgH;

//stop the player falling
imgSpeedY = 0;

//allow jumping again
jumping = false;
} else {
//gravity accelerates the movement speed
imgSpeedY ++;
}
}

if (override == true) {
constraint = false;
}

if (constraint == true) {
imgX2 = constrain(imgX, 100, width - 100);
imgY2 = constrain(imgY, 550, height - 100);
}
useCloud();
cloud1 -= 2;
cloud2 -= 2;
cloud3 -= 2;
cloud4 -= 2;
cloud5 -= 2;
fill(0, 0, 0);
text("Survive for 100 seconds!", 830, 50);
text(str(timer.time()/1000) + " seconds", 920, 100);
if (timer.time()/1000 >= 80) {
scene = "win";
lock6 = true;
}

for (int i = 0; i < e.length; i++) {
e[i].eagle();
e[i].move();
}
for (int i = 0; i < f.length; i++) {
f[i].fishHook();
f[i].move();
}
for (int i = 0; i < e.length; i++) {
if (dist(imgX, imgY, e[i].x - 33, e[i].y + 88) < 30) {
scene = "lose";
lose = " were eaten by an eagle!";
loseImage = pic10;
}
}
for (int i = 0; i < f.length; i++) {
if (dist(imgX, imgY, f[i].x - 161, f[i].y + 117) < 30) {
scene = "lose";
lose = " were caught by a fisherman!";
loseImage = pic23;
}
}
break;
}
``````
• instead of opening new discussions: try to write a runnable sketch that shows your problem and post this.

the code above is not runnable.

moreover please describe your problem. whats happening now falsely, what do you want it to happen?

jumping: this consists of accelerating in -y direction.

this is then slowed by gravity so that the acceleration gets positive again so the character falls down.

look at the examples, gravity is implemented there and use the idea I laid out

• TFguy showed this principle above in his sketch above i think

• I have a fish, in water and there is a constraint on it but I need to override the constraint to allow the fish to jump and honestly, I am not sure where to start...

• what do you mean by constraint? when he jumps, put the constraint on hold, switch it back on afterwards

• Yes