Area2d's are passing through eachother on specific frames

joemoejoemoe Posts: 4Member
edited January 17 in Programming

I am building some sort of mario like game and I am having a tough time with the stomp mechanic where the player jumps on an enemy and they die and the player bounces off.

I have things working with the exception of one bug, the player, at times, falls through the enemy's hitbox and goes undetected. Then the player dies as if it just ran into the enemy. Here is an image to illustrate better whats going:

the blueboxes are kinematicbodys with collision shapes. The red and green rectangles are area2ds. Red is for the player and green for the enemy. What is the best way to detect the green box entering the red box if its possible to pass through it between frames? Am I using the right method with area2ds to detect such collisions? Should I be using Raycast2ds? I am a little stumped on this one as I haven't really seen any tutorials that cover this issue bug free.

Thanks.


Tags :

Comments

  • cyberealitycybereality Posts: 1,044Moderator

    That can happen if the areas move more pixels in one frame than the combined height of their hit boxes. It is probably better to have one large hit box for the player and also the enemy, and then do some math when they collide. For example, you can take the difference of their y positions to figure out if the player is above the enemy or close to the ground and then branch accordingly.

  • joemoejoemoe Posts: 4Member

    Thanks for responding, I appreciate that.

    I basically went that way too but it still doesn't solve the problem of falling through the enemies area hitbox. When you check the Y values you will check the Y before it enters and after it enters but is lower than the hitbox area Y coordinates.

    The only thing that really solves it for me is when the gravity is tuned down a little.

    Another solution I have yet to try is, I could store the players Y value on each frame. That way I can check the Y value on the previous frame to determine the direction the player was coming from.

    I could also call a function from the Enemy and pass in the players current and previous frame positions and determine where they cross and if a stomp or a hit should occur.

    I don't know the best way though. Game Development is difficult lol.

  • cyberealitycybereality Posts: 1,044Moderator
    edited January 17

    You can also try using ray casts. That should solve the problem of the objects missing each other. For example, shooting a ray straight downward from the player's previous position to the current position, and then checking if it hit any objects in the enemy group.

  • novaleafnovaleaf Posts: 83Member
    edited January 21

    yeah, definetely use a raycast, though in your case you can do a fake raycast by checking the previous position vectors for both areas, something like this psudocode:

    if(collisionHappened){
        if( (playerPrevious - enemyPrevious).Normalize().Y > 0.5 ){ 
            //hit from above
        }else{
            //hit from another direction
        }
    }
    

    i probably have the player/enemey flipped around, or the Y has to check Y<-0.5, but you get the idea.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file