# How to make spikes

**URL:** <https://discourse.processing.org/t/how-to-make-spikes/17935>\
**Category:** Coding Questions\
**Created:** [February 19, 2020, 3:31pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935 "2020-02-19T15:31:33Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 20, 2020, 4:14pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/22 "2020-02-20T16:14:09Z")

</div>

> [@anon68884268](#):
>
> `ha.vel = veloctiy` , because `ha.vel` is from type `PVector` and velocity from type `float` !

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

---

<div class="post-metadata">

**Author:** ![DMTR](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DMTR](https://discourse.processing.org/u/DMTR)\
**Post date:** [February 20, 2020, 4:22pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/23 "2020-02-20T16:22:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 20, 2020, 4:25pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/24 "2020-02-20T16:25:37Z")

</div>

ha.vel.set(velocity, 0);

Or so

---

<div class="post-metadata">

**Author:** ![DMTR](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DMTR](https://discourse.processing.org/u/DMTR)\
**Post date:** [February 20, 2020, 4:26pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/25 "2020-02-20T16:26:31Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 20, 2020, 5:04pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/26 "2020-02-20T17:04:52Z")

</div>

Problem is that you have velocity and vel.

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

---

<div class="post-metadata">

**Author:** ![DMTR](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DMTR](https://discourse.processing.org/u/DMTR)\
**Post date:** [February 20, 2020, 6:20pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/27 "2020-02-20T18:20:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 20, 2020, 6:55pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/28 "2020-02-20T18:55:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 20, 2020, 7:37pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/30 "2020-02-20T19:37:49Z")

</div>

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:

```auto
/** 
 * 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

---

<div class="post-metadata">

**Author:** ![DMTR](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DMTR](https://discourse.processing.org/u/DMTR)\
**Post date:** [February 20, 2020, 7:42pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/31 "2020-02-20T19:42:48Z")

</div>

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:  
[![](https://i.gyazo.com/3a663f9f8416ab70678fcb3eb1e28be3.gif) ](https://i.gyazo.com/3a663f9f8416ab70678fcb3eb1e28be3.gif)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 20, 2020, 10:42pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/32 "2020-02-20T22:42:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 21, 2020, 11:58am UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/33 "2020-02-21T11:58:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 21, 2020, 2:41pm UTC](https://discourse.processing.org/t/how-to-make-spikes/17935/34 "2020-02-21T14:41:25Z")

</div>

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.

[Previous page](https://discourse.processing.org/t/how-to-make-spikes/17935.md?page=1)
