How do I handle per pixel detections and physics behaviors ?

Get help using Construct 2

Post » Tue Oct 21, 2014 4:16 pm

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 do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Tue Oct 21, 2014 5:10 pm

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

So instead of this
Code: Select all
for each ball
Check for collision at ball.x, ball.y
---destroy hole


You could store the old positions and check the 10 positions in between like this:
Code: Select all
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


Or you can check each pixel step in between instead of 10 steps.
Code: Select all
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


Or to make it less cpu demanding you can make it only check every 10 pixels in between like so
Code: Select all
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
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Tue Oct 21, 2014 8:16 pm

Thank you very much !
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Wed Oct 22, 2014 12:57 pm

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 :
screen1.png


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 :)
You do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Wed Oct 22, 2014 5:18 pm

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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Wed Oct 22, 2014 6:37 pm

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.
code.png


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


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 ?
You do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Thu Oct 23, 2014 6:25 pm

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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Thu Oct 23, 2014 6:56 pm

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 :)
B
12
S
7
G
7
Posts: 450
Reputation: 4,276


Return to How do I....?

Who is online

Users browsing this forum: plinkie, rayolf, SashikLV and 9 guests