Message Board


Message Board > Fenix / Bennu / Gemix / DIV > distance from an object?

January 4, 2012, 05:09
DoctorN
Whiskered
91 posts
how does one go about putting in the code for the distance from an object. I used to use map_get_pixel but I am trying to find a better way for collision. To test things out, I changed everything in my fall code to:
Code:
// Implementation of fall.    
        ii = fall;    
        if(ii > 0)
            while(ii != 0)  
                if(collision(type level_mask))
                    ii = ii + 1;
                    falling=false;
                        if(!walking);
                            player_status=0;
                        else
                            player_status=1;
                        end
                break;  
            end    
            ii = ii - 1;  
            end    
        else    
            ii = 0;  
        end


It kinda works, if I made every bit of collision (walking left and right into, hitting the ceiling, preparing to jump, etc.) that, then the character just does nothing and floats up. i was wondering if there was a way to get certain distances, like for example x and y and 16 and -16. if someone could (or at the very least show) a way to use the if(collision(type level_mask)) with distance, that would be most helpful.

here is the previous code(s) (including fall before i changed it):
Code:
        //Player Movement  
        if (key (_left))
            flags = 1;
            dir = -1;            
            ii = 0;
            while (ii <4)  
                pixel = map_get_pixel(0, levelmask, playerx-11, playery);  
                get_rgb (pixel, & red, & green, & blue);  
                if (blue<220 or x==topx)  
                    ii = 0;
                    break;  
                else  
                    if (player_status!=2 and player_status!=3 and player_status!=6 and player_status!=7) player_status=1; end
                    walking=true;
                end                
                ii = ii + 1;  
            end  
            x = x-ii;  
        end  
        if (key (_right))  
            flags = 0;
            dir = 1;
            ii = 0;
            while (ii <4)
                pixel = map_get_pixel(0, levelmask, playerx+11, playery);  
                get_rgb (pixel, & red, & green, & blue);  
                if (blue<220 or x==botx)  
                    ii = 0;
                    break;  
                else  
                    if (player_status!=2 and player_status!=3 and player_status!=6 and player_status!=7) player_status=1; end
                    walking=true;
                end    
                ii = ii + 1;
            end  
            x = x + ii;
        end 

Code:
        // Jumping  
        if (key (__A))
            if(!key_jump_is_already_pressed)
                if(gun_ready)
                    player_status=6;
                else
                    player_status=2;
                end
            falling=true;
            pixel = map_get_pixel(0, levelmask, x, y +16);    
            get_rgb (pixel, & red, & green, & blue);    
            if (blue<220)
                play_wav(jump_sound,0);
                fall =- 20;    
            end
            pixel = map_get_pixel(0, levelmask, x+6, y +16);    
            get_rgb (pixel, & red, & green, & blue);    
            if (blue<220)
                play_wav(jump_sound,0);
                fall =- 20;    
            end
            pixel = map_get_pixel(0, levelmask, x-6, y +16);    
            get_rgb (pixel, & red, & green, & blue);    
            if (blue<220)
                play_wav(jump_sound,0);
                fall =- 20;    
            end
           
        end
            key_jump_is_already_pressed = true;//as long as x is held down, this is true
          else
            key_jump_is_already_pressed = false;//once space is released, this is false          
        end 

Code:
        // Implementation of fall.    
        ii = fall;    
        if(ii > 0)
            while(ii != 0)
                pixel=map_get_pixel(0, levelmask, x+6, y + (fall-ii) +15); //falling onto the left side of the mask
                get_rgb (pixel, & red, & green, & blue);  
                if (blue<220)  
                    ii = ii + 1;
                    falling=false;
                        if(!walking);
                            player_status=0;
                        else
                            player_status=1;
                        end
                    break;  
                end
                pixel=map_get_pixel(0, levelmask, x-6, y + (fall-ii) +15); //falling onto the right side of the mask
                get_rgb (pixel, & red, & green, & blue);  
                if (blue<220)  
                    ii = ii + 1;
                    falling=false;
                        if(!walking);
                            player_status=0;
                        else
                            player_status=1;
                        end
                    break;  
                end
                pixel=map_get_pixel(0, levelmask, x, y + (fall-ii) +15); //falling onto the right side of the mask
                get_rgb (pixel, & red, & green, & blue);  
                if (blue<220)  
                    ii = ii + 1;
                    falling=false;
                        if(!walking);
                            player_status=0;
                        else
                            player_status=1;
                        end
                    break;  
                end
               
                ii = ii - 1;  
            end    
        else    
            ii = 0;  
        end
   
        // Move the fall until we can.    
        y = y + (fall-ii); 
        // Acceleration of gravity    
        if (ii == 0)    
            fall = fall + 3;    
            falling=true;  
        else
            fall = 0;
        end
       
        //Hitting the ceiling
        pixel = map_get_pixel(0, levelmask, x+6, y-15); //for the far left side 
        get_rgb (pixel, & red, & green, & blue);    
            if (blue>220) fall = 3; end
        pixel = map_get_pixel(0, levelmask, x-6, y-15); //for the far right side  
        get_rgb (pixel, & red, & green, & blue);    
            if (blue>220) fall = 3; end
        pixel = map_get_pixel(0, levelmask, x, y-15); //for the far right side  
        get_rgb (pixel, & red, & green, & blue);    
            if (blue>220) fall = 3; end


PROBLEMS:
*If you are right under a platform(a 32 height hallway), you jump right through.
*Sometimes you can sidewalk up the walls
*if the ceiling "stairs" you get caught on it
*sometime if you land at an angel, you will be partially in the platform and slowly rise up properly

I was hoping a collison box would work like you said but it didnt
Code:
collision_box(type player_collision_box);
That is the code I am using.
the player is 32x32 and the collision box is 18x24 so I don't know whats wrong
____________
#
January 5, 2012, 01:12
PB
Defender of the faith
630 posts

Ok, well I didn't do any testing on this, and I only skimmed your code for some understanding of what you try to achieve. This is of the top of my head how I remeber things in DIV work, so I might be a bit off. It's just a suggestion of a way how you could implement your logic.

On a side note, I'd like to advice to use constans for the values you assign to player_status, because it clarifies the code. For instance it would read "player_status = WALKING_STATUS;".

Also the falling speed should not be more than 32 pixels, because that is the height of the map you detect collision on. If you want to fall faster, you need to iterate your collision code multiple times within a frame. So the fall speed remaints less than 32 pixels, but you call for instance frame(50) in stead of frame; (which is frame(100); ) to run the code in between twice per frame (the parameter is the percentage that is added, when reaching 100, the frame is rendered.

DIV code:
   //If colliding, a reference to the colliding process is returned:
    collided_obj = collision(TYPE level_mask);

    //check if a reference took place. (loop over all found colliding objects in this frame)
    //Always make sure you do not try to access local variables on an empty reference!
    WHILE(collided_obj)
        //To illustrate simple behaviour implementation, I just check if there is an object somewhere on any x below this process:
        IF(collided_obj.y > y - fall + ii)
            //Some logic copied from your source which I think you'd want here
            falling = false;
            IF(!walking);
                player_status=0;
            ELSE
                player_status=1;
            END
        ELSE IF(collided_obj.y < y - fall + ii)
            //when hitting something above you:
            //Some logic copied from your source which I think you'd want here
            fall = 3;
        END
        //Find any other colliding level_mask processes
        collided_obj = collision(TYPE level_mask);
    END


[Edited on January 5, 2012 by PB]
____________
#
January 5, 2012, 05:31
DoctorN
Whiskered
91 posts
game compiles but locks once the player is loaded. I changed the code to this
Code:
collide_level = collision(type level_mask);
        // Implementation of fall.    
        ii = fall;    
        if(ii > 0)
            while(ii != 0)
                //check if a reference took place. (loop over all found colliding objects in this frame)
                //Always make sure you do not try to access local variables on an empty reference!
                while(collide_level)
                    //To illustrate simple behaviour implementation, I just check if there is an object somewhere on any x below this process:
                    if(collide_level.y > y - fall + ii)
                        ii = ii + 1;
                        //Some logic copied from your source which I think you'd want here
                        falling = false;
                        if(!walking);
                            player_status=0;
                        else
                            player_status=1;
                        end
                        break;
                    else if(collide_level.y < y - fall + ii)
                        //when hitting something above you:
                        //Some logic copied from your source which I think you'd want here
                        fall = 3;
                        break;
                    end
                    ii = ii - 1;  
                end
            end    
        else    
            ii = 0;  
        end 


EDIT: this may be the problem but I am not sure. If you try to move on the axis by a point, the game freezes, whole numbers, it doesn't.

[Edited on January 5, 2012 by DoctorN]
____________
#
January 7, 2012, 02:58
PB
Defender of the faith
630 posts

I see you use many iterators, but no frame statements within them. In DIV this would freeze the game once you're in an infinate loop without a frame statement to render the result...
____________
#
January 7, 2012, 22:56
DoctorN
Whiskered
91 posts
well how do i do that?
Code:
collide_level = collision(type level_mask);
        // Implementation of fall.    
        ii = fall;    
        if(ii > 0)
            while(ii != 0 and !collide_level)
                    //To illustrate simple behaviour implementation, I just check if there is an object somewhere on any x below this process:
                    if(collide_level.y > y - fall + ii)
                        ii = ii + 1;
                        //Some logic copied from your source which I think you'd want here
                        falling = false;
                            if(!walking);
                                player_status=0;
                            else
                                player_status=1;
                            end
                        break;
                    else if(collide_level.y < y - fall + ii)
                        //when hitting something above you:
                        //Some logic copied from your source which I think you'd want here
                        fall = 3;
                        break;
                    end
                    ii = ii - 1;  
            end    
        else    
            ii = 0;  
        end
    
        // Move the fall until we can.    
        y = y + (fall-ii); 


I get the error
Quote:
ERROR: Runtime error in Player<65545> - process 0 not active


[Edited on January 7, 2012 by DoctorN]
____________
#
January 9, 2012, 01:14
PB
Defender of the faith
630 posts

You're actually trying to access an empty reference:

Code:
while(ii != 0 and !collide_level)

should be:
Code:
while(ii != 0 and collide_level)

____________
#
January 9, 2012, 05:16
DoctorN
Whiskered
91 posts
character just sinks into the floor
____________
#
January 9, 2012, 21:46
PB
Defender of the faith
630 posts

Change

Code:
y = y + (fall-ii); 


into

Code:
if(falling)
    y = y + max(-31, min(fall-ii, 31));
end


To make sure the character doesn't fall more than 31 pixels and doesn't fall if falling is false.

Because I didn't really look into the ii variable stuff, there might be some problem in that algorithm as well...
____________
#
January 10, 2012, 01:17
DoctorN
Whiskered
91 posts
I really would like some solid done help. How about I email you the code? min and max don't work at all. I even made them variables but the game refuses to compile with them.
____________
#
January 10, 2012, 03:18
DoctorN
Whiskered
91 posts
if i do .y at the end of collide_level the game crashes in the if or while
____________
#
January 10, 2012, 22:59
PB
Defender of the faith
630 posts

Just PM me a link and I'll look at it:
http://booleansoup.com/index.p … mpose&to=PB

[Edited on January 10, 2012 by PB]
____________
#
January 11, 2012, 07:22
DoctorN
Whiskered
91 posts
that link didnt work, i uploaded some .prg's here
http://forum.bennugd.org/index.php?topic=2900.0
____________
#
January 11, 2012, 21:31
PB
Defender of the faith
630 posts

Ok, I think this site should work: http://www.speedyshare.com/

You can upload a zip file containing the entire game (the folder which contains everything, including the graphics and prg files). Then share the download link here.

I think that should work.
____________
#
January 11, 2012, 23:53
DoctorN
Whiskered
91 posts
http://www.speedyshare.com/file/Qa2Bx/GADGET.7z
____________
#
January 13, 2012, 05:50
DoctorN
Whiskered
91 posts
so?
____________
#
January 13, 2012, 20:39
PB
Defender of the faith
630 posts

I've succesfully downloaded it, but I haven't used Bennu yet, so I wasn't able to compile yet. I expect somewhere this weekend to have time to look at it more...
____________
#
January 14, 2012, 06:04
DoctorN
Whiskered
91 posts
use bennupack for windows and if you use fedora, use my tutorial in the documentation section of the bennugd forums to set it all up
____________
#
January 21, 2012, 18:20
DoctorN
Whiskered
91 posts
I got structures to work but I still need better collision
____________
#
January 30, 2012, 17:48
DoctorN
Whiskered
91 posts
my patience is growing thin
____________
#
January 30, 2012, 21:36
Zomg
None
641 posts
DoctorN: when I was working on my game project a few years ago, I came across this neat developers book on collision detection. This book is focused on that subject:

http://www.amazon.com/Real-Tim … 5791&sr=8-1
____________
#
January 31, 2012, 01:19
DoctorN
Whiskered
91 posts
I dont have the $50 for a book nor does bennu capable of that
____________
#
January 31, 2012, 09:26
Dennis
どこかにいる
2092 posts

THANKS ANYWAY FOR LOOKING IT UP

oh wait, that was not me that was supposed to say it.
I guess you're just pissed because you had a reply and you can't do anything with it. Sigh...


Quoting PB:
Change

Code:
y = y + (fall-ii); 


into

Code:
if(falling)
    y = y + max(-31, min(fall-ii, 31));
end


To make sure the character doesn't fall more than 31 pixels and doesn't fall if falling is false.

Because I didn't really look into the ii variable stuff, there might be some problem in that algorithm as well...


how about:

Fenix code:
while(detect_floor)
  y = y--;
end


I used while loops without frames a lot to correct my character getting into walls and floors and stuff. It corrects but you can't see it since there is no frame statement.
____________
Kwakkel
#
January 31, 2012, 20:34
DoctorN
Whiskered
91 posts
could you test that in the code i uploaded?
____________
#
February 8, 2012, 11:34
Dennis
どこかにいる
2092 posts

No, I don't have Bennu or Fenix installed. :(


I also wonder. The thing youa re trying to do is so common I wonder no one else programmed this before. In that case it would be easier to do "get_distance_from_object(id,id2)"
____________
Kwakkel
#
February 8, 2012, 11:48
Zomg
None
641 posts
I get that a lot when working with other people. They get pissed at me because they don't have my overpowering knowledge.
____________
#

Page 1 , 2


Message Board > Fenix / Bennu / Gemix / DIV > distance from an object?

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!