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

Username:   Password: 
 RegisterRegister   
 Multiplatform collision and shorter code.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Gooie




PostPosted: Sat Jan 12, 2008 12:04 am   Post subject: Multiplatform collision and shorter code.

I decided a few days ago that It would be a lot easier to make a platformer system right now, and just add to it, rather than reinventing the wheel every time I need a nice movement system. So, I started, I learned about velocity and gravity, also I figured out how to add in a variable friction. I also had a nice plan done for having multiple platforms, to jump on. But, I hit a snag, I have it so that all of the platforms made automatically add their positions to an array of a record to check with when the sprites are figuring out gravity, and collision.

The problem is that if I put the check for ground to disable gravity in a for loop, it allows it through because it only checks one value at a time, I need it to meet all the values at once, for every step of movement. There is probably something obvious here. But I lack the experience to say.

P.S. I realize there are some unfinished systems, and that I do need to check the X values for that platform as well. I just want the Y working first before I move on.

I also wouldn't mind getting ideas for shortening the code. If you have any idea's not matter how fragmented, I'd like to hear them.

Thanks in advanced, any help I get that attributes to a working system I will divide all of my bits amongst all the helpers.

P.P.S. The Sprite can be an AI character of a human, the four players were just a test.

Turing:
var window : int :=
    Window.Open ("nobuttonbar, graphics:500;500, offscreenonly")

var Key : array char of boolean

% These varibales are used to check all Platform X, Y values
% with the character X, Y values for gravity collision, and run
% collision.
var PlatformRecord : flexible array 1 .. 0 of
    record
        BottomX, TopX, BottomY, TopY : int
    end record
var SpriteRecord : flexible array 1 .. 0 of
    record
        Height, Width, PositionX, PositionY : int
    end record

const GRAVITY := 1
const FRICTION := 1
const AIRRESISTANCE := 3

% Sprite Template
class NewSprite

    % I/O for class
    import Draw, Key, PlatformRecord, GRAVITY, FRICTION, SpriteRecord
    export Initialize, Control, Apply, OutputVisual

    % Variable Declarations
    var PositionX, PositionY, VelocityX, VelocityY,
        Height, Width, Density, Mass, Color, Shape,
        MovementType, RunSpeed, Acceleration, JumpForce : int := 0

    % This should not be called in a loop.
    procedure Initialize (PositionX_, PositionY_, Height_, Width_, Color_,
            Density_, Shape_, MovementType_, RunSpeed_, JumpForce_ : int)
        % Set Values For Class
        new SpriteRecord, upper (SpriteRecord) + 1
        SpriteRecord (upper (SpriteRecord)).Height := Height_
        SpriteRecord (upper (SpriteRecord)).Width := Width_
        SpriteRecord (upper (SpriteRecord)).PositionX := PositionX_
        SpriteRecord (upper (PlatformRecord)).PositionY := PositionY_
        MovementType := MovementType_
        Mass := Height_ * Width_
        PositionX := PositionX_
        PositionY := PositionY_
        Density := Density_
        Height := Height_
        Width := Width_
        Color := Color_
        Shape := Shape_
        RunSpeed := RunSpeed_
        JumpForce := JumpForce_
    end Initialize

    % Based on MovementType entered, each type will be initiated.
    procedure Control
        /* Movement Types:
         0 - Player Controlled (WASD)
         1 - Player Controlled (TFGH)
         2 - Player Controlled (IJKL)
         3 - Player Controlled (Arrow Keys)
         */

        case MovementType of
            label 0 :
                if Key ('d') then
                    for i : 1 .. RunSpeed
                        VelocityX += 1
                    end for
                elsif Key ('a') then
                    for i : 1 .. RunSpeed
                        VelocityX -= 1
                    end for
                end if
                if Key ('w') and
                        PlatformRecord (1).TopY = PositionY then
                    VelocityY += JumpForce
                end if

            label 1 :
                if Key ('h') then
                    for i : 1 .. RunSpeed
                        VelocityX += 1
                    end for
                elsif Key ('f') then
                    for i : 1 .. RunSpeed
                        VelocityX -= 1
                    end for
                end if
                if Key ('t') and
                        PlatformRecord (1).TopY = PositionY then
                    VelocityY += JumpForce
                end if

            label 2 :
                if Key ('l') then
                    for i : 1 .. RunSpeed
                        VelocityX += 1
                    end for
                elsif Key ('j') then
                    for i : 1 .. RunSpeed
                        VelocityX -= 1
                    end for
                end if
                if Key ('i') and
                        PlatformRecord (1).TopY = PositionY then
                    VelocityY += JumpForce
                end if

            label 3 :
                if Key (KEY_RIGHT_ARROW) then
                    for i : 1 .. RunSpeed
                        VelocityX += 1
                    end for
                elsif Key (KEY_LEFT_ARROW) then
                    for i : 1 .. RunSpeed
                        VelocityX -= 1
                    end for
                end if
                if Key (KEY_UP_ARROW) and
                        PlatformRecord (1).TopY = PositionY then
                    VelocityY += JumpForce
                end if
        end case
    end Control

    % Apply Physics, and Collision to forces and objects.
    procedure Apply
        if PlatformRecord (1).TopY not= PositionY then
            VelocityY -= GRAVITY
        end if
        for i : 1 .. -VelocityY
            if PlatformRecord (1).TopY not= PositionY then
                PositionY -= 1
                if PlatformRecord (1).TopY = PositionY then
                    VelocityY := 0
                end if
            end if
        end for
        if VelocityY > 0 then
            for i : 1 .. VelocityY
                PositionY += 1
            end for
        end if
        if VelocityX > 0 then
            VelocityX -= FRICTION
        elsif VelocityX < 0 then
            VelocityX += FRICTION
        end if
        PositionX += VelocityX
    end Apply

    % Draw Character
    procedure OutputVisual
        /* Shapes :
         0 - Square
         1 - Circle
         2 - Star
         3 - Maple Leaf
         4 - Picture (R&D) */

        case Shape of
            label 0 :
                Draw.FillBox (PositionX - round (Width / 2),
                    PositionY, PositionX + round (Width / 2),
                    PositionY + Height, Color)
            label 1 :
                Draw.FillOval (PositionX, PositionY + round (Height / 2),
                    round (Width / 2), round (Height / 2), Color)
            label 2 :
                Draw.FillStar (PositionX - round (Width / 2),
                    PositionY, PositionX + round (Width / 2),
                    PositionY + Height, Color)
            label 3 :
                Draw.FillMapleLeaf (PositionX - round (Width / 2),
                    PositionY, PositionX + round (Width / 2),
                    PositionY + Height, Color)
        end case
    end OutputVisual
end NewSprite




% Plateform Template
class NewPlatform

    % I/O for class
    import Draw, PlatformRecord
    export Initialize, DrawPlatform

    % Variable Declarations
    var BottomX, BottomY, TopX, TopY, Color : int

    % This should not be called in a loop.
    procedure Initialize (BottomX_, BottomY_, TopX_, TopY_, Color_ : int)
        new PlatformRecord, upper (PlatformRecord) + 1
        PlatformRecord (upper (PlatformRecord)).TopY := TopY_
        PlatformRecord (upper (PlatformRecord)).TopX := TopX_
        PlatformRecord (upper (PlatformRecord)).BottomX := BottomX_
        PlatformRecord (upper (PlatformRecord)).BottomY := BottomY_
        TopX := TopX_
        TopY := TopY_
        BottomX := BottomX_
        BottomY := BottomY_
        Color := Color_
    end Initialize

    % This should be called in a loop.
    procedure DrawPlatform
        Draw.Box (BottomX, BottomY, TopX, TopY, Color)
    end DrawPlatform
end NewPlatform

var Ground : ^NewPlatform
new Ground
Ground -> Initialize (0, 0, maxx, 100, green)

% Create Sprites
var Player1, Player2, Player3, Player4 : ^NewSprite
new Player1
new Player2
new Player3
new Player4
Player1 -> Initialize (100, 500, 10, 10, red, 4, 2, 0, 4, 15)
Player2 -> Initialize (200, 500, 10, 10, blue, 4, 1, 1, 2, 30)
Player3 -> Initialize (300, 500, 2, 2, green, 4, 1, 2, 2, 20)
Player4 -> Initialize (400, 500, 30, 30, yellow, 4, 1, 3, 2, 15)

loop
    Input.KeyDown (Key)
    Player1 -> Control
    Player1 -> Apply
    Player1 -> OutputVisual
    Player2 -> Control
    Player2 -> Apply
    Player2 -> OutputVisual
    Player3 -> Control
    Player3 -> Apply
    Player3 -> OutputVisual
    Player4 -> Control
    Player4 -> Apply
    Player4 -> OutputVisual
    Ground -> DrawPlatform
    View.Update
    Draw.Cls
    delay (30)
end loop

Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Sat Jan 12, 2008 9:36 am   Post subject: RE:Multiplatform collision and shorter code.

What should we be using when we have x variables all with the same name, but different numbers at the end of their name? Wink
Gooie




PostPosted: Wed Jan 16, 2008 7:36 pm   Post subject: Re: Multiplatform collision and shorter code.

Ok, I have been toiling over a hot keyboard for 4 days. Still I have no idea how I'm going to do this. The problem is how I check for collision, I step through the Y Velocity and add 1 or subtract 1 from the Y Position. This is to avoid snapping up when the Velocity is too great and it passes the line. It's like an absolute stop. Here is the code I have a problem with:

Turing:

        %GRAVITY
        for i : 1 .. upper (PlatformRecord)
            if PositionY not= PlatformRecord (i).PositionY then
                OnGround := false
                VelocityY -= 1
            else
                OnGround := true
            end if
        end for

        % HANDLES VERTICLE MOVEMENT
        for i : 1 .. abs (VelocityY)
            if VelocityY > 0 then
                PositionY += 1
            elsif VelocityY < 0 then
                PositionY -= 1
            end if
        end for



How can I check for the values of more than one platform. If it's already in a for loop. I need to combine these sections into one. But, if in that case I would check one at a time. Allowing him to fall for the millisecond its checking another record. But if I keep them separate and use an OnGround Boolean. Then I can't check every position.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: