Make object run up ramps

So, I am trying to make a 2D game where there is a ball going on forever and there is a black line dividing the screen in two halfs (upper half and lower half). There are also some objects that the ball must dodge. the only thing the User does is Click that make the ball change the half that it is located. I would like though to add ramps that the ball can run on so it gets more interesting. any ideas ?

1 Like

Here we see a picture that is helpful.

Normally the ball (green) is moving along the ground (black).

That means its center (purple) follows a straight path (yellow).

But when it gets close to a ramp (red), it needs to stop following the straight path at some point (pink), and instead follow a path over the ramp (orange).

Once it gets over the ramp, it falls back down and lands on the ground again, at which point (blue) it starts following the straight path again.

Be cause the red ramp has a fixed width, you can determine (based on its location) the points at which the ball needs to have its path adjusted due to the ramp.

… This is, of course, assuming your ball has no momentum / you’re not using a physics model to move your ball. If it does, why, then simply apply a force upward along the normal of the ramp when the ball is touching it.

… Helping you more is hard without seeing some example code of a ball.

1 Like

Well I see the idea… (Hello again btw) The only thing is that if I try o implement this I have to change the whole code which is not that hard as it isn’t big. Though based on the following code, what do you suggest. (on thing is that the ball is not following a certain path and there is no gravity yet as it wouldn’t climb anywhere)

Obstacles class:

class Obstacles{
  PVector pos = new PVector();
  PVector dim = new PVector();

  
  Obstacles(float x, float h, float w){
    pos.x=x;
    pos.y=0;
    dim.x=w;
    dim.y=h;
  }
  void drawRect(){
    stroke(0);
    fill(0);
    rect(pos.x,pos.y,dim.x,dim.y);
  }
  
  boolean rectHit(){
    if(ball.pos.x+size/2>pos.x && ball.pos.x-size/2<dim.x+pos.x && ball.pos.y==side*size/2){
      return true;
    }
    return false;
  }

  float sideInit(){
   side = random(0,1);
   if(side>0.5){return 1;}
   return -1;
  }
}

ball class (Runner):

class Runner{
  PVector pos = new PVector();
  
  Runner(float x,float y){
    pos.x=x;
    pos.y=y;
  }
  
  void show(){
   fill(255,0,0);
   stroke(0,0,255);
   ellipse(pos.x,pos.y,size,size); 
   //b = loadImage("ball.png");
   //image(b,pos.x-size/2,pos.y-size/2,size,size);
  }
  
  void invert(){
    pos.y*=-1; 
  }
}

Main program:

Runner ball;
//PImage b;
Obstacles obj = new Obstacles(0,0,0);
int size=50;
float offx=0;
float side;

void setup(){
 size(640,640);  
 translate(-offx,height/2); 
 ball = new Runner(size/2+width/6.4,size/2);
 side = obj.sideInit();
 obj = new Obstacles(width,side*random(50,90),random(size/2,100));
 background(255);
 strokeWeight(4);
 frameRate(120);
}

void draw(){
 translate(-offx,height/2);
 background(255);   
 obj.drawRect();
 ball.show();
 stroke(0);
 line(0,0,width+offx,0);
 ball.pos.x+=4;
 offx+=4;
 if (obj.rectHit()){noLoop();}
 if (obj.pos.x<offx){side=obj.sideInit();obj = new Obstacles(width+offx,side*random(50,90),random(size/2,100));}
}

void mousePressed(){
 ball.invert(); 
}

void  keyPressed(){
 if(keyCode==32){ball.invert();}
}

Thanks in advance :slight_smile:

1 Like

So will the gravity be arcade gravity, or more like physics simulation gravity? What is the control scheme like – left right arrows, hitting, rubber band dragging? Will there jumping?

Between creating or using a full physics sim on the one hand (complex) and having a piecewise height collision function on the other (simple), the question is how abstract and simple your solution should be.

Well I will use arcade gravity, like y-- until hit ground or sth. The controls are pretty simple. Click to change the side you are. (explained in description) So the ball will run into a ramp. no jumping no nothing. just continuous movement forward.