Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Adding initial horizontal motion into a jumping procedure
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mayb123




PostPosted: Tue Nov 20, 2007 1:13 pm   Post subject: Adding initial horizontal motion into a jumping procedure

i am trying to allow for horizontal running velocity to be incorporated into the jump procedure giving a trajectory with a slight 2-3 pixel restraint on how much you can affect the trajectory by holding down the a or d keys (wasd controls). for example's sake, i took out the variables that require external files and replaced the character with a dot. any help would be greatly appreciated. thanks

code:
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 := 0.06
const decel := 0.95
const mcrad := 5
var platformx1 : array 1 .. 6 of int := init (50, 100, 150, 200, 250, 300)
var platformx2 : array 1 .. 6 of int := init (60, 110, 160, 210, 260, 310)
var platformy1 : array 1 .. 6 of int := init (50, 100, 150, 200, 250, 300)
var platformy2 : array 1 .. 6 of int := init (60, 110, 160, 210, 260, 310)


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 := .5
vy := 0


% PROCEDURES

procedure samplelevel
    for i : 1 .. 6
        drawfillbox (platformx1 (i), platformy1 (i), platformx2 (i), platformy2 (i), black)
    end for
end samplelevel
procedure movement
    if key ('d') and vx <= 5 and y = 10 then
        vx += 0.059
        pos := 4
    elsif key ('a') and vx >= -5 and y = 10 then
        vx -= 0.059
        pos := 2
    else
        vx *= decel
    end if
end movement
procedure velocity
    x += vx
    y += vy
end velocity
procedure jump %fix this part by adding horizontal motion
    if y > mcrad then
        vy -= gravity
    else
        vy := 0
    end if
    if vy = 0 and key ('w') then
        vy := 2
    end if
    % if key ('w') then
    %     vy += ax
    %
    % % end if
    % % if round (y) > 10 then
    % %     ax += gravity %Fix these
    % % end if
    % if round (y) <= 0 then
    %     vy := 0
    % end if
end jump
% procedure right
%     if key (KEY_RIGHT_ARROW) and vx <= 5 and y = 10 then
%         vx += 0.099
%         pos := 4
%     else
%         vx *= decel
%     end if
% end right
% procedure left
%     if key (KEY_LEFT_ARROW) and vx >= -5 and y = 10 then
%         vx -= 0.099
%         pos := 2
%     else
%         vx *= decel
%     end if
% end left
procedure background
    drawfillbox (0, 0, maxx, maxy, black)
    drawfillbox (10, 10, 630, 470, white)
end background
procedure endofscreendetection
    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
end endofscreendetection


%IMPLEMENTATION




loop
    Input.KeyDown (key)
    cls
    movement
    endofscreendetection
    jump %comment this to allow horizontal motion using a and d keys
    velocity
    drawfilloval (round (x), round (y), 2, 2, black)
    % Pic.Draw (chief (pos), round (x), round (y), picMerge)
    delay (10)
    View.Update
end loop
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Tue Nov 20, 2007 3:28 pm   Post subject: Re: Adding initial horizontal motion into a jumping procedure

Well, it really depends on you game. Do you want it to be realistic, do you want it to be like mario, etc.

Generally, the faster someone is moving before jumping the further horizontally they will go. This suggests you should divide the x velocity on ground right before the jump by some constant and use the quotient as the new x velocity in air. The velocity of the object in the air is not affected by ground friction, so you should disable that while in mid-flight. You may choose to add air resistance, but it will be VERY small so you can also ignore it.


The rest is up to you and your game. When the character lands you probably want to set all velocities to 0 (unless the character is on skates, where he would keep going on ground).

You may want the user to be able to affect the 'take-off'. To do this check if the user is pressing the appropriate direction at the 'instant' the jump key is pressed. If they are, then you add more to the x velocity in that direction. If they aren't (they let go and then press jump key, you may infer that they want to jump straight up, but will likely still have some x velocity). You may also want to make a forward jump not have as much y velocity as a straight-up jump, because the person is splitting their jump force to go both forward and up.

In mario-like games the user can effect the velocity of the character while in mid-air. I don't think someone can do this in real life, but it still might be good for your game.

There are a number of other things that you can put in as well (that make it more fun or more realistic).
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: