1

So, I'm working on a 2D physics engine, and I have an issue. I'm having a hard time conceptualizing how you would calculate this:

Take two squares:They move, collide, and at some vector based off of the velocity of the two + the shape of them.

I have two vector lists(2D double lists) that represent these two shapes, how does one get the normal vector?

The hit vector is just (s1 is the first shape, s2 the second) s2 - s1 in terms of the position of the center of mass.

Now, I know a normal vector is one perpendicular to an edge, and I know that you can get the perpendicular vector of a line by 90 degrees, but what edge?

I read in several places, it is the edge a corner collided on. How do you determine this?

It just makes no sense to me, how you would mathematically or programmatically determine what edge.

Can anyone point out what I'm doing wrong in my understanding? Sorry for providing no code to explain this, as I'm having an issue writing the code for it in the first place.

hammus
  • 2,602
  • 2
  • 19
  • 37
user2507230
  • 562
  • 1
  • 3
  • 12
  • Maybe [this](http://stackoverflow.com/questions/13261767/java-ball-object-doesnt-bounce-off-of-drawn-rectangles-like-its-supposed-to/13263022#13263022) might help... – MadProgrammer Dec 12 '13 at 03:25
  • Detection doesn't seem to be an issue, it's a matter of handling the detection. – user2507230 Dec 12 '13 at 03:31

1 Answers1

4

enter image description hereenter image description hereenter image description here

Figure1: In 2D the normal vector is perpendicular to the tangent line:

Figure2: In 3D the normal vector is perpindicular to the tangent plane

Figure3: For a square the normal vector is easy if you are not at a corner; It is just perpendicular to the side of the square (in the image above, n = 1 i + 0 j, for any point along the right side of the square).


However, at a corner it becomes a little more difficult because the tangent is not well-defined (in terms of derivatives, the tangent is discontinuous at the corner, so perpendicular is ambiguous).

enter image description here

Even though the normal vector is not defined at a corner, it is defined directly to the left and right of it. Therefore, you can use the average of those two normals (n1 and n2) as the normal at a corner.

To be less technical, the normal vector will be in the direction from the center of the square to the corner of the collision.


EDIT: To answer the OP's further questions in the chat below: "How do you calculate the normal vector for a generic collision between two polygons s1 and s2 by only knowing the intersecting vertices."

In general, you can calculate the norm like this (N is total verts, m is verts inside collision):

vcenter = (∑N vi) / N

vcollision = (∑m vi) / m

n = vcollision - vcenter

enter image description here

  1. Fig. 1 - vcollision is only a single vertex.
  2. Fig. 2 - vcollision is avg of two verts.
  3. Fig. 3 - vcollision for generic polygon intersection.
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • That helps quite a bit, but this implies that you know which edge the collision involves, all I know is what vertices are inside another shape? – user2507230 Dec 12 '13 at 04:18
  • At most you will detect 2 verts inside the other shape, then the norm is perpendicular to the line connecting those two points. If you only detect one vert inside the other shape, the norm is from the center of that shape, to the vert itself (as in the last figure). – bcorso Dec 12 '13 at 04:21
  • That more or less gave me an epiphany, but what if there are many verticies, such as a circle converted into a polygon? In one tick of motion, their could be several verticies inside, say, a square? Of course, for a circle you would treat it as a singular vertex, but what if you don't know it's a circle? – user2507230 Dec 12 '13 at 04:23
  • In general, you could take the average position of all verts contained inside the other shape, call it v_avg, and the normal would be from the center of the shape to v_avg. – bcorso Dec 12 '13 at 04:29
  • You, my friend, are a genius. Thanks so much! :) – user2507230 Dec 12 '13 at 04:30