How do I make the 8-Direction behavior slide against walls ?

Get help using Construct 2

Post » Fri Mar 27, 2015 12:23 am

How do I make the 8-Direction behavior slide against walls ?

I've found @R0J0hound 's example (event_motion2.capx), and works great for what I need (slide on 45° objects), but I would like to keep the 8-Direction behavior for the acceleration and deceleration.
viewtopic.php?f=147&t=103339&p=775018&hilit=wall+sliding#p775018

How can I implement the following mechanics in the game:
Image

I've tried different ways with collision points but no success so far ... Can someone help me ?
B
47
S
15
G
6
Posts: 497
Reputation: 7,064

Post » Sat Mar 28, 2015 5:58 am

You can use the 8direction behavior to move the object but you can't use the solid behavior for the walls because the behavior will just stop when hitting a solid.

At this point we can detect collisions with the overlap condition. The part we want now is some kind of collision response to keep the object out of the walls and do other stuff like sliding.

The simplest collision response would be to save the object's position before moving and the if it overlaps a wall after moving then undo the move by moving back to the saved position. The drawback is this won't do any sliding and if moving fast the object will stop short of the wall.

Another method is to move the object a pixel or so at a time until it's not overlapping. All that's needed is a direction. If we use the opposite direction of the motion we'll get the object to stop at the edge of the wall, but not slide. If we instead find the closest direction to move out we can get sliding. We just need a good way to find the closest direction.

One idea to find the closest direction would be to save the object's position then try moving out in each of the four directions of the wall, and measure the distance moved for each. The closest direction would be the one with the shortest distance. You'll want to do this for each wall. With this you'll get wall sliding but you can get some jumping when hitting a corner which may be kind of like your third picture but not quite.

One thing to consider is so far all that's being done is correcting the position of the object, but we will also need to correct the velocity of the object. Sliding would basically be setting the velocity perpendicular to the side of wall (or normal) to zero. The normal is also the same as the closest direction that the object moved out.

Setting the velocity along a direction to zero can be done with some math.
Var vx= object.velocityX
Var vy= object.velocityY
Var dot= vx*cos(dir)+vy*sin(dir)
Set velocity to (vx-dot*cos(dir), vy-dot*sin(dir))

Dot is the velocity along a direction and we're subtracting it from the velocity.


You can avoid the position jumping by not moving out in the closest direction. Instead move out in the opposite direction, keeping track of how far we move out as this is the remaining distance to move. Then we need a way to find the normal of the collision. From that we can the correct the speed as with above and then try to move again with the new velocity, or basically just move in the same direction of the current edge with the remaining distance. This can be repeated as many times as nessisary but usually it's fine to do it once.

One way to do the normal detection is to use overlaps at offset to check the positions around the object
Var dx=0
Var dy=0

Overlaps at offset(1,0)
--- add 1 to dx
Overlaps at offset(1,1)
--- add 1 to dx
--- add 1 to dy
Overlaps at offset(0,1)
--- add 1 to dy
Overlaps at offset(-1,1)
--- add -1 to dx
--- add 1 to dy
...etc

Then the normal should equal angle(dx,dy,0,0)

This requires that the object is all the way pushed out first before finding the normal.

All this will cause a very high amount of collision checks and the object may jitter when repeatedly pushing out of walls due to pushing out a pixel at a time not being precise enough. Both can be solved by implimenting raycasting which can be used to find exact collision points and makes finding the normal simpler, that is once it's all setup which isn't really simple.

Of course I may be overthinking a lot of this, and even after getting wall sliding working a few times I think it could be made a lot simpler. Instead of handling everything with equations to handle all cases you probably could get by by coming up with a list of cases and handling them one by one. For instance the third case in your image to move around a corner. For example:

object is moving down
Wall is below
---wall not below to the left
------ move object left
---wall not below to the right
------- move object right

Just do that for all four directions. Still you'll still need some good collision response like above.

Anyway I hope some of the above is useful in some way.
B
84
S
27
G
70
Posts: 4,933
Reputation: 49,558

Post » Mon Apr 06, 2015 7:44 pm

Here's a capx for wall sliding with the 8dir behavior.
https://dl.dropboxusercontent.com/u/542 ... slide.capx

All it is is a superior way to push the player out of solids than what the behavior does by default. Well, actually solid isn't used so that we can handle collision response ourselves instead of letting the behavior do it. The player is treated as a circle (which solves your third image), and the walls are all rectangles. The algorithm just calculates the closest corner or edge on each wall and moves out in the opposite direction. The velocity isn't messed with and it appears to work well enough without doing that.

It has no dependence on the 8 behavior so it could be used in other ways. All that it needs is a player sprite and box sprites for walls with four image points at the corners.
B
84
S
27
G
70
Posts: 4,933
Reputation: 49,558

Post » Tue Apr 21, 2015 6:35 pm

@R0J0hound
Sorry for my late reply, I was busy with with my job and college.
Thank you very much for your example and explanation, it is what I needed :)
B
47
S
15
G
6
Posts: 497
Reputation: 7,064

Post » Tue Nov 03, 2015 6:15 am

@Tokinsom @megatronx and anyone else I linked here before.

Update:
Here's a more generic version of my above capx:
https://dl.dropboxusercontent.com/u/542 ... _plus.capx
It no longer uses imagepoints, instead it calculates the corners regardless where the object's origin is. Also it's not tied to a particular sprite anymore, but you do have to duplicate an event or two to support more types (documented in capx).
B
84
S
27
G
70
Posts: 4,933
Reputation: 49,558

Post » Tue Nov 03, 2015 12:13 pm

R0J0hound wrote:@Tokinsom @megatronx and anyone else I linked here before.

Update:
Here's a more generic version of my above capx:
https://dl.dropboxusercontent.com/u/542 ... _plus.capx
It no longer uses imagepoints, instead it calculates the corners regardless where the object's origin is. Also it's not tied to a particular sprite anymore, but you do have to duplicate an event or two to support more types (documented in capx).


Thanks, looks interesting. I needed one for isometric game with rectangular polygons, so this is how I did mine a while ago, which pushes out, but is not making a nice smooth slide yet but it needs some smoothing out on pushing out:

https://drive.google.com/file/d/0B0jUjW ... sp=sharing
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
73
S
20
G
19
Posts: 1,931
Reputation: 17,084

Post » Tue Nov 03, 2015 6:39 pm

@megatronx
Here's the capx changed in a different way. It still uses imagepoints, but as long as there is three or more, and the points define a convex shape it will work. For isometric you could position the points in a diamond shape.
https://dl.dropboxusercontent.com/u/542 ... _poly.capx
B
84
S
27
G
70
Posts: 4,933
Reputation: 49,558

Post » Tue Nov 03, 2015 8:06 pm

R0J0hound wrote:@megatronx
Here's the capx changed in a different way. It still uses imagepoints, but as long as there is three or more, and the points define a convex shape it will work. For isometric you could position the points in a diamond shape.
https://dl.dropboxusercontent.com/u/542 ... _poly.capx


Looks cool,thought it is much more complex then mine. How would you go about using this engine with several types of collisions:
with walls that are not being pushed out
with enemies that are pushed out
with other object that can be pushed out, but differently by enemies and differently by the player

I know it's a lot, and I have done all of those, but I'm curious how this would go with your engine.
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
73
S
20
G
19
Posts: 1,931
Reputation: 17,084

Post » Tue Nov 03, 2015 8:10 pm

This new .capx is great @R0j0hound Love how it finds its way around sharp corners too; that was another feature I was hoping to figure out! Thanks again
Image
B
230
S
27
G
13
Posts: 1,781
Reputation: 18,163

Post » Tue Nov 03, 2015 8:49 pm

Thank you very much @R0J0hound!

I learn more reading your replies in the "How do I" section, than I do from reading any other source.

It seems like every day you show us something new and magical.

Thank you.
B
28
S
8
G
4
Posts: 767
Reputation: 5,940

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 1 guest