We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've recently created a program and I'd like to be able to allow my spirte to jump. I'm able to move it left and right sucessfully but I just can't get the jumping to work. I'll post my code below and if anyone could help it would be greatly appricated (I've only started coding 2 months ago so sorry if its a stupid question ). Lines of interes possibly: 84 = Gravity and introduction of sy. 112 = Controls. 197 = Colisions.
//import ddf.minim.analysis.*;
//import ddf.minim.*;
//Minim minim;
//AudioPlayer Tune;
PImage sprite1;//, sprite2;
int jcount = 0; //Jump Count
int shotl = 0;
int shotr = 0;
float x=0.0; // X Position
float y=1.0; // Initial position
float sy=0; //Mapped y
float frame=0; //Frame Count
int direction=0; //Direction Facing
int fchange=0; //Frame Change
float g=-9.81; // Gravity
float k=0.25; // Spring constant
float m=1; // Mass kg
float dt=0.01; // Time step 50ms
float vy=0;// Initial velocity
float t=0; // Initial time
int shoot=0;
int travell=0;
int travelr=0;
void setup()
{
size(1280,720, P2D);
//Music Loading
/*minim = new Minim(this);
Tune = minim.loadFile("Tune.wav");
Tune.loop();
*/
//Image Loading
sprite1 = loadImage("Sprite1.jpg");
//sprite2 = loadImage("Sprite2.jpg");
textureMode(NORMAL);
blendMode(BLEND);
noStroke();
}
void draw()
{
background(0,200,255);
//Sun
fill(255,255,0);
ellipse(100,100,100,100);
//Top Platform
fill(255,0,0);
rect(490,310,300,30);
//Middle Platform Left
fill(255,0,0);
rect(0,500,300,30);
//Middle Platform Middle
fill(255,0,0);
rect(590, 500, 100, 30);
//Middle Platform Right
fill(255,0,0);
rect(980,500,300,30);
//Middle Colum
fill(255,0,0);
rect(625,340,30,160);
//Bottom Platform
fill(255,0,0);
rect(0,690,1280,30);
//Gravity
vy=vy+(g-((k/m)*vy))*dt;
y=y+(vy*dt);
t=t+dt;
sy=map(y,0,1,height-1,0);
//Player
float left = frame/2;
float right = (frame+1)/2;
if(direction==1)
{
float temp = left;
left = right;
right = temp;
}
//Draw Player
pushMatrix();
translate(x,sy);
beginShape();
texture(sprite1);
vertex(0,0,right, 0);
vertex(51,0,left,0);
vertex(51,75,left,1);
vertex(0,75,right,1);
endShape(CLOSE);
popMatrix();
if(keyPressed)
{
if(keyCode==UP)
{
while(jcount<=10)
{
sy=sy-20;
jcount++;
}
}
if((keyCode==RIGHT)&(x!=width-50))
{
direction=1;
x+=10;
if(fchange == 10)
{
frame++;
if(frame==2) frame=0;
fchange=fchange-10;
}
fchange++;
}
if((keyCode==LEFT)&(x!=0))
{
direction = 0;
x-=10;
if(fchange == 10)
{
frame++;
if(frame==2) frame=0;
fchange=fchange-10;
}
fchange++;
}
if(keyCode==DOWN)
{
g=g-1;
}
if(keyCode==SHIFT)
{
shoot=1;
}
}
//Bullet
float bx = x;
if((shoot==1)&(direction==1))
{
shotr=1;
shoot=0;
}
if((shoot==1)&(direction==0))
{
shotl=1;
shoot=0;
}
fill(87);
if(shotr==1)
{
rect(bx+50,sy+30, 20, 10);
while(travelr<=80)
{
bx=bx+20;
travelr++;
}
shotr=0;
travelr=0;
}
if(shotl==1)
{
rect(bx-20, sy+30, 20, 10);
while(travell<=80)
{
bx=bx-20;;
travell++;
}
shotl=0;
travell=0;
}
//Collisions
if(sy+75>=690)
{
dt=0;
}
}
Answers
Jumping thing example:
There are 6 variables that control the movement of the thing. (px,py) is the position. (vx,vy) is the thing's current velocity. (ax,ay) is the thing's acceleration. Notice that there is a constant acceleration in the y direction (gravity). notice that a jump simply causes the y velocity to suddenly be set to a negative value (you're moving up immediately). Try jumping in the air! Whoops, did you fall off the line? It's okay.