0 Favourites

How do I handle per pixel detections and physics behaviors ?

  • Hi.

    I'm working on a game like Worms.

    I'm using the Canvas plugin to create the destructible landscape, but I encounters some difficulties : if the missile moves very fast, it moves through the landscape before destroying it... Any idea how to fix this ?

    You can see what I'm talking about if you change the Y velocity on the fourth condition.

  • You'll have to check the positions between the timesteps.

    So instead of this

    for each ball
    Check for collision at ball.x, ball.y
    ---destroy hole[/code:xvtk3yei]
    
    You could store the old positions and check the 10 positions in between like this:
    [code:xvtk3yei]for each ball
    repeat 10 times
    Check for collision at lerp(ball.oldx, ball.x, loopindex/10), lerp(ball.oldy, ball.y, loopindex/10)
    --- destroy hole
    
    every tick
    --or--
    ball: on created
    ---ball: set oldx to self.x
    ---ball: set oldy to self.y[/code:xvtk3yei]
    
    Or you can check each pixel step in between instead of 10 steps.
    [code:xvtk3yei]global number dist=0
    
    for each ball
    --- set dist to distance(ball.oldx, ball.oldy, ball.x, ball.y)
    ------ repeat dist times
    ------ Check for collision at lerp(ball.oldx, ball.x, loopindex/dist), lerp(ball.oldy, ball.y, loopindex/dist)
    --------- destroy hole
    
    every tick
    --or--
    ball: on created
    ---ball: set oldx to self.x
    ---ball: set oldy to self.y[/code:xvtk3yei]
    
    Or to make it less cpu demanding you can make it only check every 10 pixels in between like so
    [code:xvtk3yei]global number dist=0
    
    for each ball
    --- set dist to distance(ball.oldx, ball.oldy, ball.x, ball.y)/10
    ------ repeat dist times
    ------ Check for collision at lerp(ball.oldx, ball.x, loopindex/dist), lerp(ball.oldy, ball.y, loopindex/dist)
    --------- destroy hole
    
    every tick
    --or--
    ball: on created
    ---ball: set oldx to self.x
    ---ball: set oldy to self.y[/code:xvtk3yei]
  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Thank you very much !

  • The first solution suits me well and it's amazingly more accurate than my previous code. I tried the second one but it seems to be very CPU demanding, no way I can use it for my game.

    However, there is still an issue with the first one : the missile still passes through this instead of colliding :

    [attachment=1:2cta8zyl][/attachment:2cta8zyl]

    I tried to increase the loop iterations numbers to 100, 500 or 5000, but it doesn't change anything.

    Any idea ? You can try it on my capx, don't change the current angle and press the N key to shoot at high speed.

    It probably won't be a real issue on the gameplay because we won't shoot at these speed, but I like to learn and understand

  • Here is you example tweaked a bit.

    https://dl.dropboxusercontent.com/u/542 ... tweak.capx

    Change steps to change the number of in between positions to check.

  • Thank you.

    When I increases the steps in your file (for example 50 steps), a single projectile fired with the N key creates 3 holes, that's weird.

    I have another question for you !

    As you can see on my capx, the vehicule falls (physic behavior) and stops its fall when it encounters a non transparent pixel.

    I have the same problem I had with the projectiles : because of the fall speed, it doesn't stops on the first non transparent pixel encountered during its trajectory.

    Simply put, it will fall through a thin ground.

    To avoid this issue, I tried your second solution checking each pixel during the trajectory.

    It doesn't work as expected on my file.

    Here, I used this to create a 1 pixel tall blue bar for each pixel during the vehicule fall, but it appears that it doesn't check every pixel during the trajectory.

    [attachment=2:28xq0nyh][/attachment:28xq0nyh]

    The red line is part of the landscape and the vehicule should land on it.

    [attachment=1:28xq0nyh][/attachment:28xq0nyh]

    So, it obviously misses some non transparent pixels on this trajectory, which causes the vehicule to passes through them.

    Any idea about how to avoid that ?

    Last question, this time about my projectile again. So I used your first solution, which is nice. I use 10 iterations.

    I noticed my computer starts to be sluggish with only 5 shots fired on the screen. Is it supposed to be that CPU demanding, or is there some serious optimization work to do in my events ?

  • The problem of 3 holes being made is solved by adding a stop loop action when the terrain it hit.

    My second idea is flawed when I tried it myself, it would need a bit of tweaking to get it to work. The general idea is to move a pixel at a time until either terrain is encountered or the new position is reached.

  • I managed to properly use the first solution (there was an issue in my previous events) for my falling vehicules and it's accurate enough to land on a 1px tall ground. Thank you very much for your help

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)