Perfect player collision example with source

Started by MisterN, February 05, 2012, 11:22:24 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MisterN

Thank you for all your help, however I was unhappy with collision, until now. I think it is reasonable to pay back to the community (where did they go? its like empty here in the english section). So here is a very basic platform engine player colliding with the blocks perfectly (read the comments).
werg

handsource-dyko

Works fine!
About structures, you can also put the block objects into an array.




MisterN

#2
I need an adaptive size of arrays but I dont know to place an array, let alone a structure. I know how to use, load and save structures, but nothing more than that. someone told me to do like

block[0] = block(x,y);
block[0].x = x_coordinate;
block[0].y = y_coordinate;


but the "block[0] = block(x,y);" part did not work. How can I just make it that I use a process (its a cursor that moves along a grid at 16x16) and when I press the "placement" button, it spawns the block process (which spawns the different collision process points) there, and from there, I can save it all into a structre, and it can be loaded in my main game?

Ive literally been asking this question everyday for like 6 months.

-----------------------
EDIT: I may have figured it out :O
//Block placement
process block_placement()
private
int block[300];
begin
block[0] = block(0,0);
end


or not?
werg

handsource-dyko

What they told you is kinda pointless, especially the first line. You can also start with a simple array in memory from code. The load/save part comes later.

An adaptive array is way too complecated, just stick with a fixed map size. All the old systems like the Nes and C64 had fixed limits. I suggest limiting maps to 6400 * 800 pixel, thus 400*50 tiles of 16*16. So you will have at most 5120000 blocks. With processes that kould be a huge memory hog, but with map_put you could make it more memory efficient, but less dynamic. But for simplicty let's stick with a 320x200 pixel map. So the array will be 20*15.



byte levelmap [20],[15];



Each block in this map can be a number between 0 and 255, so your tileset can be 256 different tiles (0=empty tile).
I, know defining the map in code is a lot of typing, but it's a start.

You can define the data directly in the array definition, like data statements in basic.
The numbers represent graphic codes from an fpg file. Be carefull with the data type byte,
as they cannot be negative and not be bigger then 255. If your tileset fpg has codes beyond 255,
you should use short integers.


byte levelmap [20],[15]= 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,

                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,

                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
                         0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0;


This is an empty map of 20*15 tiles. To display it, just create a nested for loop that creates processes or does something with map_put().


MisterN

the levels it the most would be 2560x1440 or [160],[90]

and would:

struct block
    int block[300];
    int x;
    int y;
end


//Block placement
process block_placement()
private
begin
    block.block[0] = block(16,16);
end

work or does it need to be worked on?
werg

handsource-dyko

What you show would also work, but I suggest:


struct block [300];
     bool used;
     int graph; // or byte graph (requires less memory, as an int takes 4 bytes)
     int x;
     int y;
end



process block_placement();
private

int counter;

begin
     for  (counter=0; counter<301; counter+=1)
          if (block[counter].used==true)
              tileblock (block[counter].graph,block[counter].x, block[counter].y);
          end
     end
end


Note, I named the block process "tileblock", to avoid a naming conflict with the "block" structure.

Futu-block

to put one image that you upload by this page, with the ritgh buttom copy the url, then edit your post and paste with img labels:

example:

MisterN

Ok thanks. I dont mean on releasing the whole ending, most of it is easy except for player collision which i got nailed down! I just want newcomers to learn from this lol.
werg

MisterN

It works as far as I know but the game comes to a halt after awhile:
//Block placement
process block_placement(x,y);
private
int counter;
begin
z = -255;
x+=8; y+=8;
graph = load_png("./obj/block/placer.png");
    loop
        //block placement
        for  (counter=0; counter<301; counter+=1)
            if(collision_box(type block))
                block[counter].used=true;
            else
                block[counter].used=false;
            end
            if(key(_control))
                if (block[counter].used!=true)
                    block[counter].x = x-8; block[counter].y = y-8;
                    block (block[counter].x, block[counter].y);
                end
            end
        end
       
        //moving
        if(key(_w))
            if(up_key!=true)
                y-=16;
            end
            up_key = true;
        else
            up_key = false;
        end
        if(key(_a))
            if(left_key!=true)
                x-=16;
            end
            left_key = true;
        else
            left_key = false;
        end
        if(key(_s))
            if(down_key!=true)
                y+=16;
            end
            down_key = true;
        else
            down_key = false;
        end
        if(key(_d))
            if(right_key!=true)
                x+=16;
            end
            right_key = true;
        else
            right_key = false;
        end
           
    frame; end
end


any ideas how that can be improved?
werg

handsource-dyko

Try to find out how much memory your program is using while it's running. Just open the windows task manager and watch bgdi's memory usage as the program runs. If it keeps getting bigger, and execeed the memory limitations of the Dreamcast, then you know you've got a memory leak.
I did these tests a lot on malvado to find potential problems that could cause crashes. This way you can find out how much memory a program actaully needs to run. Malvado won't run on the DC because it uses 25+ mb on avarage.

MisterN

actually from what I saw its just spawning many blocks per second in the same spot no matter what. and thats slowing the game down.
werg

MisterN

This at least ensures no memory leaks (a block will constantly check if another block is right under it and will delete the old one) until you can help me with something better

//Block placement
process block_placement(x,y);
private
int counter;
begin
z = -255;
x+=8; y+=8;
graph = load_png("./obj/block/placer.png");
    loop
        //block placement
        for  (counter=0; counter<301; counter+=1)
            if(collision(type block))
                block[counter].used=true;
            else
                block[counter].used=false;
            end
            if(key(_control))
                if(place_key!=true)
                    if (block[counter].used!=true)
                        block[counter].x = x-8; block[counter].y = y-8;
                        block (block[counter].x, block[counter].y);
                    end
                end
                place_key=true;
            else
                place_key=false;
            end
        end
       
        //moving
        if(key(_w))
            if(up_key!=true)
                y-=16;
            end
            up_key = true;
        else
            up_key = false;
        end
        if(key(_a))
            if(left_key!=true)
                x-=16;
            end
            left_key = true;
        else
            left_key = false;
        end
        if(key(_s))
            if(down_key!=true)
                y+=16;
            end
            down_key = true;
        else
            down_key = false;
        end
        if(key(_d))
            if(right_key!=true)
                x+=16;
            end
            right_key = true;
        else
            right_key = false;
        end
           
    frame; end
end

//Block necessary
process block(x,y)
begin
z = 0;
graph = load_png("./obj/block/block.png");
floor(x,y); wall(x,y); ceiling(x,y);
x+=8;
y+=8;
    loop
        if(collision(type block))
            signal( id , s_kill_tree ); //this prevents the overlapping of other blocks and ensures no memory leaks
        end
    frame; end
end


and my game ran fine so I do believe that the code is spawning hundreds in the same place per second. Problem is though, will that still take up alot of memory per se, or will I have to deal with it regardless? Also, a strange bug, I cannot get blocks to spawn when the placer is at x coordinate 300 (about there). any fix? thanks
werg

MisterN

See? this is where I get stuck for 6 months and nothing happens. There is literally nothing salvageable that I can incorporate into my game. And your wiki goes into very little detail for everything.
werg

MisterN

I got something to work with images, hold on a sec, if I can it to work and I can make a level editor out of it ill release that too.
werg