We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I decided to try code a traditional jump mechanic by following a video Dan made programming in p5.js using vectors and shapes and applying forces etc. Things seemed to make sense while watching his video and so I tried coding it on Processing. So far, I can make my block fly high in the sky.
Person p;
void setup() {
size(1366,768);
p = new Person(new PVector(50,height));
}
void draw() {
background(0);
gravity();
jump();
p.update();
p.checkEdges();
p.display();
}
void gravity() {
PVector gravity = new PVector(0, 3);
p.applyForce(gravity);
}
void jump() {
if (key == 'z') {
PVector jump = new PVector(0, -0.1);
p.applyForce(jump);
}
}
class Person {
PVector location;
PVector velocity;
PVector acceleration;
Person(PVector l) {
acceleration = new PVector(0,1);
velocity = new PVector(0,0);
location = l.get();
}
void applyForce(PVector force) {
acceleration = force;
}
void update() {
velocity.add(acceleration);
location.add(velocity);
}
void display() {
noStroke();
fill(127);
rect(location.x, location.y-50, 20, 50);
rectMode(CENTER);
}
void checkEdges() {
if (location.y > height) {
velocity.y *= -0;
location.y = height;
}
}
}
So Its all relatively simple but it doesnt want to stay to the ground, it keeps going up!!. Maybe theres a better way to organize my code instead of having it laid out in such a way. If anybody can help me force that block down as it jumps I would very much appreciate it a lot. In fact, any knowledge to make my life easier about sidescrolling and platformers I would very very much appreciate it!!
Heres that dan shiffman video I referred to: https://youtube.com/watch?v=Ouza_4SsbLc
Answers
It's not PVector-based, but I've got this online jump sketch example:
http://studio.ProcessingTogether.com/sp/pad/export/ro.9LZizxKsIAY4F
Got a sidescroller game, also posted online:
http://p5js.SketchPad.cc/sp/pad/view/ro.GrwKUsycNrY/latest
And its corresponding forum post:
https://forum.Processing.org/two/discussion/8997/hoppy-beaver#Item_2
thanks Ill have a look!
edit: i guess im thinking more of a platformer thing when the sprite/box naturally jumps ala super mario bros or any modern indie platformer. it seems really easy to implement in processing but ive hit a wall, and just not getting it.
This jumping method may be useful. When a key is pressed, it causes the variable loc.y to decrease and then increase, and stop when it hits the ground (there may be some mistakes in the code).
PVector loc = new PVector(0, 0); boolean jump = false, standing = true, grav = false, boolean n; float yInc = 20, a = 3; if((keyPressed) && (jump == false) && (grav == false)) { jump = true; } if (jump) { grav = false; yInc--; loc.y -= yInc; } if (grav) { loc.y += a; a += 0.1; } if (standing) { grav = false; } else if (!standing) { grav = true; } if (/*box lands on ground*/) { grav = false; standing = true; jump = false; yInc = 20; a = 3; } else { standing = false; } void keyPressed() { n = true; } void keyReleased() { n = false; }
I haven't started this yet but I intend to write a journal about the status of my platformer. I've gotten a lot further than I anticipated and intend to show others code and design ideas etc.