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

Username:   Password: 
 RegisterRegister   
 Platform collision using whatdotcolor (based off of the movement tutorial)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rcbhs




PostPosted: Tue Dec 08, 2009 1:49 pm   Post subject: Platform collision using whatdotcolor (based off of the movement tutorial)

Ok so we are trying to make collision for this movement system based off of whatdotcolor. We used the movement from the movement tutorial...however, when I reach the platform during my fall it will jump up from that platform, however I can't make the box actually land on the platform. If I take out the code I used to make it able to jump he will land, but, then he can't jump...any help would be appreciated.

code:
const GROUND_HEIGHT := 120
const RUN_SPEED := 10
const JUMP_SPEED := 30
const GRAVITY := 2.7

var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean

% player position and velocity
var posx, posy : int
var velx, vely : real

posx := 20
velx := 0
posy := 400
vely := 0

loop
    Input.KeyDown (chars)
    if chars ('q') then
        exit
    end if

    % to make the player move
    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if

    % remember, in order to jump you MUST be on the ground when pressing UP
    if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT or posy = 220 then
        vely := JUMP_SPEED
    end if

    % subtract your "gravity" constant from the velocity EACH frame
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

    % simple "collision" check. just makes sure the player stays above ground
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    elsif posy = 220 then
        posy := 220
        vely := 0
    end if


    % different colours just to illustrate whether or not you may jump
    if posy = GROUND_HEIGHT then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, green)
    else
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, red)
    end if
    drawfillbox (200, 200, 300, 220, 3)
    drawline (0, GROUND_HEIGHT, maxx, GROUND_HEIGHT, blue)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    if whatdotcolor (posx, posy) = 3 then
        posy := 220
        vely := 1
    end if

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    View.Update
    delay (25)
    cls
end loop

Window.Close (win)
Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: Tue Dec 08, 2009 2:33 pm   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

for something like falling and jumping you shouldn't be using whatdotcolour, you should probably be using linear collision detection. (if you don't know how to use it, I can write a tutorial for you!)
rcbhs




PostPosted: Wed Dec 09, 2009 8:39 am   Post subject: Re: Platform collision using whatdotcolor (based off of the movement tutorial)

We will look into linear collisio, if you write a tutorial Very Happy So umm yeah, write one up.
mirhagk




PostPosted: Wed Dec 09, 2009 9:26 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

alright I'll start working on it right now. Have you done/ do you remember linear math (like intersection of lines and stuff). If you don't, you might want to brush up on some of that because you'll be using intersections for linear collision
rcbhs




PostPosted: Wed Dec 09, 2009 11:37 pm   Post subject: Re: Platform collision using whatdotcolor (based off of the movement tutorial)

Now my question is, if we are going to have upwards of 50 platforms per level, won't linear platform collision be nothing but a TON of code? We figured with whatdotcolor we can just kinda draw a bunch of boxes throughout the level in paint and do our collision that way.
TheGuardian001




PostPosted: Thu Dec 10, 2009 1:04 am   Post subject: Re: Platform collision using whatdotcolor (based off of the movement tutorial)

This is where the magic of arrays comes into play.
Basically, instead of defining each box with its own variable, you define the lot of them under an array, then iterate through them with a for loop. You'll need to know these at some point if you want to get anywhere, so you might as well pick them up now. We have a tutorial on arrays right over here.
mirhagk




PostPosted: Thu Dec 10, 2009 9:43 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

the thing is that linear collision detection is actually very cheap in terms of speed (especially if you calculate the character's "line" first then use arrays and for loops to compare it to every other one)

Whatdotcolour is extremely ineffiecient and only allows objects to be a specific colour (or a picture with a two step drawing[not very fast at all])

I'm about half done the tutorial btw, when I'm done I'll post a link to it here k.
rcbhs




PostPosted: Mon Dec 14, 2009 8:33 am   Post subject: Re: Platform collision using whatdotcolor (based off of the movement tutorial)

TheGuardian001 @ Thu Dec 10, 2009 1:04 am wrote:
This is where the magic of arrays comes into play.
Basically, instead of defining each box with its own variable, you define the lot of them under an array, then iterate through them with a for loop. You'll need to know these at some point if you want to get anywhere, so you might as well pick them up now. We have a tutorial on arrays right over here.


So basically your saying we will still have to hard code every single platform somewhere? Unless I am thinking of liner collision differently then it actually is, we will still have to tell the program exactly what the left x and right x coordinates are...My thought of using whatdotcolo is that 1, we dont care what color the platforms are (lol), this way we can just work on other concepts of our game as opposed to platform collision.

You need to take into account here, we have like 2 more weeks to get this game going and well, we don't want to spend that time on hard coding the locations we want our platforms when we could just make identical colored platforms and use whatdotcolor...
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Dec 14, 2009 9:53 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

No hard coding would be required. The magic of arrays is such that you can read the locations of all of your platforms from a file really, really easily. You would then draw platform "tiles" at those locations, instead of having them imprinted on your background images.
mirhagk




PostPosted: Mon Dec 14, 2009 10:46 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

alright sorry I've been kinda busy lately with other stuff so I haven't been able to write that tutorial. I'll explain the basics here though. Basically what you want is an array that stores the x and y as well as the height and width of the box. Read the above tutorial on arrays so you understand it but something like this is what you want:
Turing:

type box:
record
x,y:int
h,w:int
end record
var boxes:array 1..50 of box


now to draw these boxes you'd simply do the following
Turing:

for i:1..50
drawfillbox(boxes(i).x,boxes(i).y,boxes(i).x+boxes(i).w,boxes(i).y+boxes(i).h,red)
end for

that way you can draw as many boxes as you want in just 3 lines. You can already see arrays are much more effecient.
rcbhs




PostPosted: Tue Dec 15, 2009 8:44 am   Post subject: Re: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

mirhagk @ Mon Dec 14, 2009 10:46 am wrote:
alright sorry I've been kinda busy lately with other stuff so I haven't been able to write that tutorial. I'll explain the basics here though. Basically what you want is an array that stores the x and y as well as the height and width of the box. Read the above tutorial on arrays so you understand it but something like this is what you want:
Turing:

type box:
record
x,y:int
h,w:int
end record
var boxes:array 1..50 of box


now to draw these boxes you'd simply do the following
Turing:

for i:1..50
drawfillbox(boxes(i).x,boxes(i).y,boxes(i).x+boxes(i).w,boxes(i).y+boxes(i).h,red)
end for

that way you can draw as many boxes as you want in just 3 lines. You can already see arrays are much more effecient.


Now I'm not sure if its just me (I do have this problem often) not understanding what your saying again lol. I understand how that code works (least I understand exactly what that code does), however at some point we need to set the locations of those platforms, correct? If at any point we need to set the positions of all 50+ platforms we have PER LEVEL, its not worth the time imho. We don't want random platforms, I could have made random platforms in the very beginning. Our platforms are going to be in specific positions.

Again, sorry for any confusion, I hate trying to figure stuff out via typing cause, I tend tomisunderstand what people are saying.
DemonWasp




PostPosted: Tue Dec 15, 2009 10:17 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

How do you want to specify the locations of platforms? You have two basic ways: some sort of bitmap or image, or else a data file. The second option is simpler, while the first option may make it easier to design the map.
mirhagk




PostPosted: Tue Dec 15, 2009 11:52 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

you should put the locations of the platforms into a map file. You can even create a program that lets you draw the locations on-screen and then places that location in the map file. That way you can easily design the map, while having an effecient system of drawing the platforms and checking for collisions.

Also putting the locations into map files and having them created with another program makes it very simple to add new levels, freeing up your time to do more important things.

If you don't understand how to do this, I'd be glad to help you (I'll do anything to stop a programmer from using the whatdotcolour command)
rcbhs




PostPosted: Thu Dec 17, 2009 8:36 am   Post subject: Re: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

mirhagk @ Tue Dec 15, 2009 11:52 am wrote:
you should put the locations of the platforms into a map file. You can even create a program that lets you draw the locations on-screen and then places that location in the map file. That way you can easily design the map, while having an effecient system of drawing the platforms and checking for collisions.

Also putting the locations into map files and having them created with another program makes it very simple to add new levels, freeing up your time to do more important things.

If you don't understand how to do this, I'd be glad to help you (I'll do anything to stop a programmer from using the whatdotcolour command)


So it would be easier in the long run to make a map file to place the locations (not 100% sure what you mean by mapfile btw lol)? Did you just mean I make one program to place the platform locations and one program to run the rest of my game?
DemonWasp




PostPosted: Thu Dec 17, 2009 10:31 am   Post subject: RE:Platform collision using whatdotcolor (based off of the movement tutorial)

You make one program to run your game. It includes a (relatively tiny) bit of code to read the positions of your platforms from a file. The position of each platform is nothing more than a rectangle with (x1,y1,x2,y2) coordinates. This means you can have a map file as follows:

code:

100 100 300 110
0 0 10000 10


Which would define a rectangle up in the air, near the start of the level (assuming you start at 0) and another platform (the floor) starting at the start of the map and continuing considerably past the end of the screen.

You should be able to figure out how to read this with your program.
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  [ 15 Posts ]
Jump to:   


Style:  
Search: