# Making my own physics engine (need help sorta)

Discussion in 'Physics & Math' started by gamelord, Jun 24, 2018.

1. ### gamelordRegistered Senior Member

Messages:
673
So I want to make my own (2D) physics engine. I want to have a block hit a stationary wall. It will use a system of 4 points to detect collisions (The wall will always be larger than the block.)

So I want each of the 4 points to be moving in space, the 4 points are the corners of block. Say the 4 points are moving northward, and the block is not rotating. Any ideas?

Please Register or Log in to view the hidden image!

to hide all adverts.
3. ### CptBorkRobbing the Shalebridge CradleValued Senior Member

Messages:
5,810
What happens when the block collides with the wall? Does it rotate then, or stop, or what?

to hide all adverts.
5. ### gamelordRegistered Senior Member

Messages:
673
The block is meant to imitate real life, so whatever it would do in real life. The block is made out of solid strong wood, no bending.

In the above case with the COM in the middle, the left side of the block (the 2 points furthest from the wall in the picture) should move forward and cause a clockwise angular rotation, slightly moving the contact point rightward at the end of the frame. Depending on the restitution the block (defined as 4 points) may bounce backward.

However I do not know the exact equation to compute the future position of all 4 points in the next frame.

Last edited: Jun 24, 2018

to hide all adverts.
7. ### DaveC426913Valued Senior Member

Messages:
10,904
In real life, there are no materials that don't deform. Your simulation will have a limit on how faithfully it models reality. Where that limit is set is up to you and how much complexity you're willing to put into it.

Regardless, you want to research software collision detection. It's a pretty well-developed field that defines the math of intersecting objects.

Something else to look at is physics engine software. That is the superset of collision detection, and covers all the other scenarios of how simulated objects behave.

8. ### gamelordRegistered Senior Member

Messages:
673
Yes I am not going for realism here, just basic newton rigid bodies.

I googled software collision detection but I didn't find much. Mostly I am interesting in just the physics not the actual collision detection.

The only thing I managed to find is this. https://www.toptal.com/game/video-game-physics-part-ii-collision-detection-for-solid-objects

I read it briefly but I don't think it describes the physics math, only the math of the collision detection. EDIT: It may have the answer, but it is a lot of work and complex.

Last edited: Jun 24, 2018
9. ### DaveC426913Valued Senior Member

Messages:
10,904
Sorry, you want physics? Get a block of wood and a wall.
You want software? That's math.

There is no physics in rigid body collision. It's not real, by definition.

What do you want?

10. ### gamelordRegistered Senior Member

Messages:
673
The equation, to compute the final four points in the diagram.

Now, I read that link I posted, I took more time to read it and I think it provides what I'm asking for, but it seems really long-winded and a lot of steps, and hundreds of lines of things to read.

I think there must be some kind of easy and simple equation for the 4 points that doesn't take so long to learn.

11. ### DaveC426913Valued Senior Member

Messages:
10,904
Do you need the block to rotate? Or is its centre of mass aligned with the collision point, so it bounces back without rotation?

Oh. You answered that. That's what you meant by COM.

Well, that's not simple.

12. ### gamelordRegistered Senior Member

Messages:
673
I think it is simple.

We are in an early state of science and mathematics. I am no expert at mathematics but we have just escaped from the dark ages and religous tyranny. But we still have a ways to go. 83% of america is religious. Atheletes and footballers get paid more than teachers at school. I believe we have a ways to go as a society in terms of equations.

It was just 1883 or so that they discovered quaternions, quaternions are absolutely crucial and essential for 3d.

What I think is that this 4 point thing is very simple. Possibly simpler than quaternions, and that all that is needed is for someone to discover the easy equation for it. In essence it is just the relationship of balance, between 4 points on interconnected lines. For all I know the equation is already made, but buried somewhere in the deepest recesses of the Internet.

13. ### Neddy BateValued Senior Member

Messages:
1,761
Do you already have a program written for how a round ball would bounce off the wall? If not, I suggest you start with that simpler case. You basically want the ball to reflect off the wall like the old "pong" style games.

Once you have that done, I think you want to make the entire block bounce off the wall in a similar manner, but you also want the block to begin rotating due to the one corner hitting the wall first. So it's probably just a rotation about the COM at some calculated rate. Reminds me of the old "asteroids" game, lol.

14. ### gamelordRegistered Senior Member

Messages:
673
Already did the pong thing. But I never perfected it to compute the rotational velocity.

In order to make sure my block sim works accurately, I want to set the restitution to 0 in the preliminary tests.

In order to see if your hypothesis is correct I am going to load up Box 2D and examine the rotational effect. It may be a few minutes so check back, I will probably do it before the day is done.

What I would like to know is Box 2D an accurate physics engine or is it deeply flawed in some way? Either way I want to make my own physics engine. But I ask because, is Box 2D a reliable source to compare the physics to?

15. ### DaveC426913Valued Senior Member

Messages:
10,904
If you had mentioned some of this up-front, we would have had a much better idea of where you are and what you're looking for.

16. ### gamelordRegistered Senior Member

Messages:
673
I am not perfect and I am not sure of all the details.

17. ### Neddy BateValued Senior Member

Messages:
1,761
How about this. You build upon your pong program, but have two balls instead of one. Connect the two balls with a straight line, and assume its length is fixed. The resulting code will suddenly have to account for variables for the friction on impact, the total mass, the center of mass, conservation of energy & momentum, etc. Sounds promising to me.

18. ### gamelordRegistered Senior Member

Messages:
673
Yes but what equations do I use? Because the equation would be radically different than for a single ball.

19. ### Neddy BateValued Senior Member

Messages:
1,761
If you want it accurate, it's not going to be one equation, but many.

For a quick cheat, I would focus on the COM of the two balls, (remember they are connected rigidly), and get them to bounce around.

You can always modify your code later for accuracy. But for now, we need bouncing balls!

EDIT: If you are not worried about gravity, then look for an old air hockey table, and slide some plastic rectangles around on it.

20. ### gamelordRegistered Senior Member

Messages:
673
Okay give me a bit to make a prototype, and I shall come back to you.

21. ### CptBorkRobbing the Shalebridge CradleValued Senior Member

Messages:
5,810
I think what you need is to program in the box's moment of inertia tensor, which will allow you to determine how fast the box rotates with a given amount of angular momentum, then you will need an equation for how the angular momentum of the box changes as it hits the wall. For simplification I would start with rigid motion, having the box's momentum perpendicular to the wall be instantaneously and completely reversed while parallel momentum remains unchanged, i.e. elastic collision, and the same momentum transfer would be used to calculate the change in angular momentum as $\vec{r}\times\vec{\Delta p}$, where $\vec{r}$ points from the centre of mass to the corner getting impacted.

22. ### gamelordRegistered Senior Member

Messages:
673
This may be the ticket. I take it to mean that r is the vector described as the point originating from the center of mass to the corner getting impacted, and that p is the vector of the pushback. And that the resultant angular velocity is the cross product of r and p?

However one thing that I must know for sure. If I set the restitution to zero, then the "pushback" should not be made a permanent velocity, but a translation over one frame. Thus I assume the equation would still hold in this case, and I simply define p as that one-frame pushback translation vector.

23. ### CptBorkRobbing the Shalebridge CradleValued Senior Member

Messages:
5,810
I did some more thinking about this problem, realized you need to also account for rotational energy. So maybe your collision model should be something where the total kinetic+rotational energy is conserved, but the change in velocity and rotation of the square is such that the instantaneous perpendicular velocity component of the corner hitting the wall is reversed, and the instantaneous parallel velocity remains unchanged, at the moment of impact.

You'd have three relevant variables to solve for in the collision: the rotation angle of the square, and the velocity of it's centre of mass in the x and y directions. You also have three equations: 1 equation for the conservation of total kinetic + rotational energy, 1 equation for the reflection of the impacting corner's velocity along x, and 1 equation for the instantaneous conservation of the impacting corner's velocity along y.