Howdy, Stranger!

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

How to make some sort of gravity system

edited December 2016

Hello Processing community, I'm relatively new to coding and I'm working on a little project for fun, It's a dodge game where u should jump and dodge things that come in your way, but I have a little problem with the jump mechanics and I was wondering if someone could clear it up a little for me. What I've done right now is this

float dodgerPosX, dodgerPosY;
float levelarenaPosX, levelarenaPosY;
float speed;
float gravity;

void setup() {
size(640, 360);
frameRate(200);
dodgerPosX = 300;
dodgerPosY = 10;
levelarenaPosX = 200;
levelarenaPosY = 300;
speed = 0;
gravity = 0.001;
}
void draw()
{
background(51);
text("V 1.0 (ALPHA)", 550, 350);
fill(255);
rect(dodgerPosX, dodgerPosY, 10, 10);
fill(92);
rect(levelarenaPosX, levelarenaPosY, 200, 100);
dodgerPosY = dodgerPosY + speed;
speed = speed + gravity;
if ( speed < 0.65 && dodgerPosY > height-99.5) {
speed = 0;
gravity = 0;
}
else if (dodgerPosY > height-70.5) {
println(speed);
speed = speed * -0.65;
}
}
void keyPressed()
{
if (key == 'd')
{
dodgerPosX = dodgerPosX + 25;
}
if (key == 'D')
{
dodgerPosX = dodgerPosX + 25;
}
if  (key == 'a')
{
dodgerPosX = dodgerPosX - 25;
}
if  (key == 'A')
{
dodgerPosX = dodgerPosX - 25;
}
if (key == 'w')
{
dodgerPosY = dodgerPosY - 35;
}
if (key == 'W')
{
dodgerPosY = dodgerPosY - 35;
}
if (key == 's')
{
dodgerPosY = dodgerPosY + 25;
}
if (key == 'S')
{
dodgerPosY = dodgerPosY + 25;
}
}`

If you run this you'll see that my figure falls down and lands on the platform, however as i start moving it stops the gravity scripts and it just floats in the air. I have tried a lot of different things but can't seem to work, I'm not expecting a full code, just something that can point me in the right direction :D Thanks alot BromanV

Tagged: