## [Tutorials] Making a simple 2D physics engine

Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

### Re: [Tutorials] Making a simple 2D physics engine

Thanks! Best tutorials about 2D physics!)
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

### Re: [Tutorials] Making a simple 2D physics engine

Snuux wrote:Thanks! Best tutorials about 2D physics!)
Thank you very much, I really appreciate it

Also, the reason why it took me forever to answer this is because I was waiting to have the third tutorial, in order to have more content in the same post. Naturally, as a huge procrastinator that I am, it took several hundreds of years, but it's finally here! Half-Life 3 confirmed!

I'm not posting a link here because I want you guys to check out the main post. There are some new information I want you guys to know about, so go check that out! Also, MANY, MANY THANKS for your support and feedback! In less than a month the Physics Tutorial post was the most visited post of my blog, and with your guys' boost, my blog has almost 10.000 views! So, from the depths of my heart, THANK YOU!
HugoBDesigner - my Blog

ivan
Party member
Posts: 1270
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: [Tutorials] Making a simple 2D physics engine

Great work on the tutorials, I can see that you put in a lot of effort there.

Just have a small suggestion regarding the following piece of code:

Code: Select all

obj1.speedx = math.min( math.max(obj1.speedx, -maxspeed), maxspeed)
obj1.speedy = math.min( math.max(obj1.speedy, -maxspeed), maxspeed)
It clamps each axis separately which means that diagonally moving objects can move faster.

here is an alternative approach:

Code: Select all

  local d = math.sqrt(obj1.speedx*obj1.speedx + obj1.speedy*obj1.speedy)
if d > maxspeed then
local n = 1/d * maxspeed
obj1.speedx, obj1.speedy = obj1.speedx * n, obj1.speedy * n
end
Again, sorry to nitpick, love the tutorials!

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

### Re: [Tutorials] Making a simple 2D physics engine

I usually do that way because in most games I don't mind about diagonal speed, but your approach is really nice and clever, thanks for sharing! I'll make sure to include it in the corresponding part of the tutorial and give credits for you.
Also, it's not nitpicking, as I said in the main post any suggestions are welcome, and having multiple methods for more things is always nice, so thank you very much