So (x,y) is the original vector, and (-y,x) is the vector normal to it.
Instead you want something simpler that calculates a normal vector to another vector. Unfortunately I don't have any good links to vector tutorials relating to collision detection, but I wanted to comment on a few points you brought up.įirstly, if you are working on a 2D platformer you will not be needing the cross product which only exists in 3 dimensional space. rather than just stretches of flat ground at varying angles. I'm hoping someone can point me in the direction of some tutorials which will help me get a better grasp on these concepts and also relate them back to collision detection.įor my game, I'm trying to create landscapes similar to what you would have seen in Sonic, curvy ground etc. I'm also having a difficult time relating what I know to the concepts being talked about in the tutorials I've read so far. but I'm obviously missing some important things, like defining a plane and obtaining the cross product.
I've been going through Vector maths tutorials and I am able to do things like getting a unit vector, getting a unit normal vector etc. I'm fairly new to game development (but not to programming) and I've seemingly forgotten everything but algebra from my highschool maths days. # push a bit more than needed so the shapes do not overlap in futureĭ_over_o_squared = d/np.I've started developing a 2d platformer and currently my collision system is based around axis-aligned rectangle bounds checking, which is working fine, however it's not enough for the purposes of this game. Return True and the push vector if o is a separating axis of p1 and p2. # Finding the edges and orthogonals of a polygon: `edges_of()` and `orthogonal()` Return the displacement between the geometric center of p1 and p2. If np.dot(d, mpv) > 0: # if it's the same direction, then invert Mpv = min(push_vectors, key=(lambda v: np.dot(v, v)))ĭ = centers_displacement(p1, p2) # direction from p1 to p2 # they do collide and the push_vector with the smallest length is the MPV # they do not collide and there is no push vector Separates, pv = is_separating_axis(o, p1, p2) P1 and p2 are lists of ordered pairs, the vertices of the polygons in the Return True and the MPV if the shapes collide.
The code below uses the functions `edges_of()`, `orthogonal()`, `dot()` and `is_separating_axis()`, which will be specified in the next session, but it should be somewhat intuitive what they do. Otherwise, if none of them is, the shapes overlap, and the smallest push vector of each orthogonal is the MPV. If any of those orthogonals is, the shapes do not overlap. For each edge in each shape, test if its orthogonal axis is a separating axis. For 2-dimensional polygons, this vector will lie in some of the orthogonal axes. We want the minimum displacement possible, We'll call this vector the _minimum push vector_ (MPV). To move (or push) one polygon away from the other, we also need to find a vector that, when added to the polygons position, will make the shapes not overlap. The above image shows only two of those axis, but that particular case has 9 of them (one for each edge).
If the projections don't overlap in at least one of them, the shapes do not overlap. The image below illustrates this idea.įor 2-dimensional polygons, we only need to examin the axes that are orthogonal to each of the shape's edges. We'll call this axis a _separating axis_. More technically, two convex shapes do not overlap if there exists an axis onto which the two objects' projections do not overlap. We'll call this line a _separating line_. In simple terms, the SAT states that if we can draw a straight line between two convex shapes, they do not overlap. That being said, the code is in Python and uses the numeric module `numpy`, here imported as `np`. You can read this tutorial skipping the code bits and it should still make sense. It is especially important to know what a dot product is and it's geometric interpretation. This short tutorial assumes basic knowledge of linear algebra. # Concise tutorial for 2-dimensional convex polygons collision detection using the separating axis theorem.