I am currently working on good physics for platform movement (as a test and to teach). Here is the entire source I have:
//Velocity And Movement Tester
//By: Nicholas Wolf
//Date: 1/29/2013
program velocity_test;
import "mod_proc"
import "mod_video"
import "mod_text"
import "mod_map"
import "mod_draw"
import "mod_math"
import "mod_grproc"
import "mod_key"
import "mod_wm"
import "mod_say"
global
ball_dim_x = 32;
ball_dim_y = 32;
begin
set_mode(320,240,16);
set_fps(60,0);
//spawn the ball
ball(320/2,240-(ball_dim_y/2));
loop
//press esc or click X to exit game
if(key(_esc) or exit_status)
exit("goodbye",0);
end
frame;end
end
process ball(x,y);
private
//key presses
bool key_right = false;
bool key_left = false;
bool key_jump = false;
//status
bool standing = false;
bool walking = false;
bool jumping = false;
bool falling = true;
//movements
int v = 0; //velocity
int v_min = -4; //the minimum a velocity can get
int v_max = 4; //the maximum a velocity can get
int v_timer = 0; //a counter that will steadily increase/decrease v
int v_timer_max = 4; //the max time a v_timer will set itself to
int dir = 0;
int h_speed = 1.5;
begin
graph = load_png("velocity.png");
loop
write(0,0,0,0,"STATS:");
write(0,0,8,0,"dir: "); write_var(0,56,8,0,dir);
write(0,0,16,0,"v: "); write_var(0,56,16,0,v);
write(0,0,24,0,"v_timer: "); write_var(0,56,24,0,v_timer);
//formula for moving
x+=dir*h_speed;
//move left and right
if(key(_right))
key_right=true;
else
key_right=false;
end
if(key(_left))
key_left=true;
else
key_left=false;
end
//determine dir
if(key_right) //because the right key, you are going right
dir = 1;
end
if(key_left) //because the left key, you are going left
dir = -1;
end
if((!key_right and !key_left) or (key_right and key_left)) //if neither keys or both keys, then stalemate
dir = 0;
end
//ensure you dont collide with wall
if(dir==1)
if(x>320-16)
dir = 0;
end
end
if(dir==-1)
if(x<0+16)
dir = 0;
end
end
//formula for movement along the y axis
y+=v;
//jumping and falling
//jumping
if(key(_control))
key_jump = true;
else
key_jump = false;
end
if(key_jump)
if(standing and !jumping and !falling) //to ensure we can only jump once we are on a surface
jumping = true;
standing = false;
end
end
if(jumping)
if(v>v_min) //since you are jumping, you want v to reach v_max
if(v_timer>0)
v_timer--;
else
v--;
v_timer = v_timer_max;
end
else
falling = true;
jumping = false;
end
end
//falling
if(falling)
if(v<v_max) //since you are falling, you want v to reach v_min, which is -v_max
if(v_timer>0)
v_timer--;
else
v++;
v_timer = v_timer_max;
end
end
if(y>240-(ball_dim_y/2)) //getting onto the ground
falling = false;
v = 0;
standing = true;
end
end
frame;end
end
what problems am I having?
1. Line 97:
y+=v;
Please change it to
y+=v^2
and experiment with the latter, by changing the "+" to a "-" and even the "^2" to a "*v". I would love for more than anything to use the latter like I can in my calculus class, but for some reason if move along they axis on its one (even set at 0) if I do this code. Know any way of fixing it?
2. Line 134-138:
if(y>240-(ball_dim_y/2)) //getting onto the ground
falling = false;
v = 0;
standing = true;
end
I would like it so the ball does not sink into the ground however I am almost certain that is because "v" holds a different value each and every time you hit the ground I just have no idea about executing it that way.
3. Run the game and look at the top left corner, you will notice that "v" is the opposite of what I set it to be as well as the "v_timer". I have no idea what went wrong in that. The mechanics work, I just have no idea why it's the opposite and I would like to fix that as soon as possible.
I have included two attachments, one being the image used in game, and the other is the program being run. I hope to see some help soon :). Thanks