In a timestep (dt seconds) the representative point moves at speed V, trying to go from location P to location P+V*dt if the segment between these locations intersects walls you can find the position Q=P+V*t1 of the first such intersection, figure out a new movement speed V2 and move the entity again, from Q with velocity V2 for a time dt-t1. "Movement vectors"? Shouldn't you use true velocities? If anyone has done this before or has seen a good tutorial on something like this it would be really helpful, thanks. I'm working with 360 degree movement, so i've got X and Y movement vectors if that helps. Maybe this could be done by recursively calling the algorithm with the new point you're moving, and returning no movement at all if it goes more than a second or third sub-call. You don't want to run up against one wall that bends inward then end up in the middle of another wall. I suspect an easy pitfall of any such algorithm would be corners, as well. Perhaps it should it be a projection of the movement vector on the wall?Ĭ. How much should i move along the wall given the wall facing and character movement vectors.
How to set the sprite's position so that it'll be on the permiter of this wall (Note that it'll always be a straight line). As the sprite runs north against the wall, he should start moving northwest along the wall, not just stop running as soon as he hits the wall because you can move where you're trying to go.Ī. that is, say a sprite is moving north, and there's a wall facing southwest. But what gets me is the "clamping" movement to the wall. Well, that's not quite the problem i can tell if a dot crosses a line easily enough. So its really just point and line intersections on a 2d plane. Meanwhile the 2d sprite's position is represented by a point. the 3d world is really an evenly spaced set of vertices in 2d with height values, and the Z doesnt matter in this case. I've got a 2d sprite moving around in a 3d world.
Implementing this library into your code will be much easier and more efficent after reading it!
His code is CC BY-NC-SA 4.0, so, this is too! I highly, highly, reccomend reading his book to better understand all of the details involved in collision detection. P5.collide2D contains some versions of, and references to, the functions in Jeffrey Thompson's Collision Detection Book. P5.collide2D provides tools for calculating collision detection for 2D geometry with p5.js. A 2d collision detection library for p5.js