Computer Science Canada

Stopping an object from falling through what is supposed to be the ground

Author:  Mayb123 [ Thu Oct 11, 2007 7:18 pm ]
Post subject:  Stopping an object from falling through what is supposed to be the ground

I am still making the game from earlier posts by me and i'm working on the section of the code to be used for motion and jumping, but before i go any further, i'm just wondering how i would go about stopping an object, in this case a dot, from falling beneath the initial y position of 10, while also being able to jump from the second position after the first jump:
Quote:

setscreen ("graphics:640;480")
var key : array char of boolean
var ground, vcap, x, y, vx, vy, ax, g, charx, chary, carvx : real
const gravity := -0.5
x := 10
y := 10
vx := 1
ax := .8
vy := 5
% g := -0.5
vcap := 10.1
procedure jump
% Makes character jump at the press of the up key.
loop
cls
View.Set ("offscreenonly")
x += vx
y += vy
if round (y)>=10 then
vy += gravity
end if
drawfilloval (round (x), round (y), 2, 2, black)
delay (10)
View.Update

end loop
end jump
procedure crouch
% Makes character crouch on the ground.

end crouch
loop
View.Set ("offscreenonly")
Input.KeyDown (key)
if key (KEY_UP_ARROW) then
jump
elsif key (KEY_DOWN_ARROW) then
crouch
end if
end loop

Author:  Saad [ Thu Oct 11, 2007 7:20 pm ]
Post subject:  RE:Stopping an object from falling through what is supposed to be the ground

Your jump procedure has no exit condition, add exit when y <= -10 and reset vy when it happens

Author:  Mayb123 [ Thu Oct 11, 2007 7:28 pm ]
Post subject:  Re: Stopping an object from falling through what is supposed to be the ground

true. i blame my negligence. thanks

Author:  Mayb123 [ Mon Oct 22, 2007 12:44 pm ]
Post subject:  Re: RE:Stopping an object from falling through what is supposed to be the ground

Saad @ Thu Oct 11, 2007 7:20 pm wrote:
Your jump procedure has no exit condition, add exit when y <= -10 and reset vy when it happens


how would i reset the variable? here's the code:
Quote:
setscreen ("offscreenonly")
setscreen ("nobuttonbar")
setscreen ("graphics:640;480")
var key : array char of boolean
var chief : array 1 .. 5 of int
var ground, vcap, x, y, vx, vy, ax, g, charx, chary, carvx : real
const gravity := -6
const decel := 0.95
var pos : int:=3
chief (1) := Pic.FileNew ("MC sprite lsiderun.bmp")
chief (2) := Pic.FileNew ("MC sprite lside.bmp")
chief (3) := Pic.FileNew ("MC sprite.bmp")
chief (4) := Pic.FileNew ("MC sprite rside.bmp")
chief (5) := Pic.FileNew ("MC sprite rsiderun.bmp")
x := 10
y := 10
vx := 0
ax := .1
vy := 0


loop
Input.KeyDown (key)
cls
if key (KEY_RIGHT_ARROW) and vx <= 5 then
vx += 0.099
pos := 4
else
vx *= decel
end if
if key (KEY_LEFT_ARROW) and vx >= -5 then
vx -= 0.099
pos := 2
else
vx *= decel
end if
if key (KEY_UP_ARROW) then % Here's the code portion
vy := 5
end if
if round (y) >= 10 then
vy +=gravity
end if
if y = 10 then
vy := 0
end if % Ends here

if round (x) >= 633 and round (x) <= 640 then
x := 8
end if
if round (x) <= 7 and round (x) >= 0 then
x := 632
end if
x += vx
y += vy

% drawfilloval (round (x), round (y), 2, 2, black)
Pic.Draw (chief (pos), round (x), round (y), picMerge)
delay (10)
View.Update
end loop

Author:  Saad [ Mon Oct 22, 2007 2:10 pm ]
Post subject:  RE:Stopping an object from falling through what is supposed to be the ground

When you exit the loop add
vy := <initial y velocity>
y := <initial height before jump


: