[Tutorials] Making a simple 2D physics engine

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

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

Post by Tjakka5 »

Have you tested this engine with objects of different sized?
At the moment I'm trying to get a 32x32 object to collide with a 8*64 object and its... not working out.

I might've just derped somewhere, but I would like to know if it works fine for you.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post by HugoBDesigner »

Yes, it is. I'm doing the tutorials based off an engine I made for Love Jam #2, and from the last time I tested everything was working fine. If you're getting a crash, or if it's not working with other objects either, add the code here so I can help you :)

Oh, by the way, Tutorial #3 is almost ready. Posting it this week.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

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

Post by Tjakka5 »

This is the the code that handles all the collision, I know it´s a bit shabby, but it was (almost) doing the job fine.
When 2 objects of the same size and width were colliding everything worked fine apart for some unrelated things. However, objects with different sizes did not as can be seen in the video linked below.

http://pastebin.com/iU2itP3p

https://www.youtube.com/watch?v=Opjqag- ... e=youtu.be

If you want I can also send you the complete project, but I must warn you, the code is quite shabby in some places. I haven't been optimizing it all that much.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post by HugoBDesigner »

Based on the video, I think I know what's happening. You're using the collision side detection that checks the width/height of the collision rectangle, right?
(in the tutorial, it's named "Collision side detection: Method 1 - Intersection rectangle measuring")

Well, I had the same problem when I was using this method in the first versions of my engine. I highly recommend you switching the Method 2 ("Collision side detection: Method 2 - Middle-point distances"). It is a lot easier, in my opinion. I'm considering adding a third method to that tutorial, though, but for now method 2 should work. If it doesn't, just let me know :)

EDIT: Nevermind, I realized the problem is in method 2. I'll make sure to edit the first tutorial to have the method 3 (which should fix these problems).
@HugoBDesigner - Twitter
HugoBDesigner - Blog
lippo
Prole
Posts: 8
Joined: Sat Oct 18, 2014 2:40 pm

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

Post by lippo »

hello hugo i have try to use your code for my training type this

Code: Select all

box = class:new()

function box:init(x, y)
self.x = x
self.y = y
self.width = 10
self.height = 10
self.image = myTexture
self.friction = .98
end

function box:update(dt)
self.x = self.x+dt*5
end

function box:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end

but so I do not know how to invoke a request on the main
then I modified box so

Code: Select all

box = {}

function box:new(x, y)
	local object = {
	x = x,
	y = y,
	width = 0,
	height = 0,
	image = myTexture,
	friction = 0
	}
	setmetatable(object, {__index = box})
	return object
end

function box:update(dt)
	self.x = self.x+dt*5
end

in to the main

Code: Select all

require 'box'

function love.load()
	--init box
	b = box:new()--istanza di classe box
	b.x = 200
	b.y = 200
	b.whidth = 30
	b.height = 30
	b.image = image
	b.friction = 10

end

function love.update(dt)
	b:update(dt)
end


function love.draw()
	b.image = love.graphics.rectangle("fill",b.x,b.y,b.whidth,b.height)

end

so it works
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post by HugoBDesigner »

The "class" box didn't work because you're not using a class library. As I said in my tutorial you'll need THIS or something similar. Then you'll call it as object:new(), even though the loading function is object:init().
@HugoBDesigner - Twitter
HugoBDesigner - Blog
lippo
Prole
Posts: 8
Joined: Sat Oct 18, 2014 2:40 pm

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

Post by lippo »

HugoBDesigner wrote:The "class" box didn't work because you're not using a class library. As I said in my tutorial you'll need THIS or something similar. Then you'll call it as object:new(), even though the loading function is object:init().
thanks for your reply hugo, ok i try to study class system for integrate your code , for making physics lib :awesome:
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

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

Post by T-Bone »

Idea: How about making a simpe .love on the first post, to give people an idea of what to expect from the kind of physics engine you can make by following your tutorials? That could help people decide if they should try something like this or just use Box2D instead.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post by HugoBDesigner »

That's actually a pretty good idea! I mean, there is enough content in all three tutorials to add a small .love file (even though the 3rd tutorial isn't released yet, it is almost finished). Thanks for the idea, man!
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post by HugoBDesigner »

So, a little update after so long:

I've updated the part 1 of the tutorials to include a third method of collision-side detection (since the other 2 methods have some cons). Thanks, Tjakka5, for pointing this out! So, if you're making a physics engine based in one of them, please check out the first post again.

I've also updated part 2 to include a segment talking about static objects (which I mentioned in part 1 but had completely forgotten in part 2). So, if you want to have static objects and such, please check out the second post again.

Part 3 is being worked on. I'll be honest, it's been quite boring, since it's more about optimization than physics properly said. It'll take a little while still, but it is 50% finished already, so it shouldn't take too much.


Also thanks for suggestion, T-bone! I'm making a game that is relatively big, but that will allow you guys to have a basic idea of what you can make in the physics engine! It'll take more time than part 3, but when it's done you'll love it (hopefully)!
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 36 guests