How to make spikes

There he said it - write something similar to this but take into account that one is a vector

hmm i dont have skill to do that :smile:. Im sorry but im just beginner so i need help with that too… :confused: I will be so happy if someone can help me

ha.vel.set(velocity, 0);

Or so

Where i put that? Global or inside jump or Hahmo. When i put it inside jump it doesnt give error but jump not work. Code i made:

 void jump() {
        velocity = height + 0.5;
        if(ypos - velocity > (height/2)) {
            ypos = height/2;
            jump = false;
            velocity = velocityconst;
            ha.vel.set(velocity, 0);
        } else {
            ypos -= velocity;
        }
    }

Problem is that you have velocity and vel.

Do you really want that? Or is it a mistake?

I dont want it like that, just tested if it works with those. So if you have/ know how to make working jump method/scirpt can you help me to make my game character jump. So we can delete that one and make new jump system if you can help me.

The core idea: get rid of velocity and then where you have it use vel throughout.

vel is inside class

It was a mistake that you started to use velocity additional to vel

Read Lightosk comment again

Actually a jump is just applying a force upwards, that should do the trick. I’ve written a little piece of code with a player class:

/** 
 * 20.02.2020 Lightosk
 * https://discourse.processing.org/t/how-to-make-spikes/17935/
 */

class Player {
  PVector pos, vel, acc;
  int size;
  color col;
  boolean grounded;
  float jumpForce;

  Player() {
    col = color(255, 0, 0);
    pos = new PVector(width/2, height/2);
    vel = new PVector();
    acc = new PVector();
    size = 32;
    grounded = true;
    jumpForce = 6.5;
  }


  void run() {
    show();
    addGravity();
    checkGround();
    update();
  }


  private void show() {
    ellipseMode(CENTER);
    noStroke();
    fill(col);
    ellipse(pos.x, pos.y, size, size);
  }


  private void update() {
    vel.add(acc);
    pos.add(vel);
    acc.mult(0); // Resetting acceleration, usually done by ForceMode, with Impulse and Force
  }


  private void checkGround() {
    println("Before checkGround: "+acc);
    println(ground, pos.y);
    if (pos.y + size / 2 >= ground) {
      println(pos.y + size / 2);
      grounded = true;
      vel.mult(0);
      acc.mult(0);
      pos.y = ground - size/2;
    } else {
      grounded = false;
    }
    println("After checkGround: "+acc);
  }


  private void addGravity() {
    applyForce(new PVector(0, GRAVITY * timeDelta));
  }


  public void applyForce(PVector force) {
    acc.add(force);
  }


  public void jump() {
    if (grounded) {
      grounded = false;
      applyForce(new PVector(0, -jumpForce));
      pos.y -= 0.1;
    }
  }
}

float GRAVITY = 9.81;

Player p;
float ground;
float timeDelta;

void setup() {
  size(600, 400);
  p = new Player();
  ground = height / 2 + 16;
}

void draw() {
  timeDelta = 1 / frameRate; // timeDelta: Multiply any value with it to convert it from per frame to per second
  background(0);
  p.run();

  stroke(0, 255, 0);
  line(0, ground, width, ground);

  if (keyPressed) {
    println(p.grounded);
    //if (key == ' ') {
      p.jump(); // Will be blocked if the player can't jump, see the variable grounded
    //}
  }
}

You can test it out without anything other, and press any key to jump

1 Like

Wow, so smooth code, i tried it and that looked amazing.

EDIT:
I converted parts of your code to mine code and i got all working!!!(i think i never publish this code because its messy but i will credit you for helping me ofcourse!)
Im so happy of this, because i was stuck on a simple thing but with my methods that was hard to make. Thanks to all who have helped with my problem. And i think i will make new post soon where i post all my coming problems on this game, or i will keep this and post here.

Game looking now:

3 Likes

It’s looking lovely!

Maybe you can make a left moving background to have the look that the player runs!!!

Also, maybe you could make the boxes look like bullets or arrows?

Great progress!

You can also hide the mouse cursor using noCursor();

You’re welcome.
Yes, you are allowed to use my code I posted, else I wouldn’t have posted it, or not? Of course a nicer look would be cool, make a new post when you’d like to graphically upgrade your game.