Page 1 of 1

Problems with the physics functions

Posted: Thu Jun 23, 2011 9:44 pm
by RP-01
Hello LÖVEers,
I'm new in development with this awesome engine, but I've already have a lot of experience in Lua. (I'm coding Lua for GMod for almost three years.)

Well.. until now I really had not any problems in what I was doing, but then I wanted to do something with the physic functions. It worked pretty good so far, but now I have some really big problems with the collision.

To cut a long story short: I wanted to spawn bouncy balls on left click (which worked so far) and boxes with collision (which doesn't work at all).
The bouncy balls bounce when they are told to do so (when they hit the window rim), but the boxes are just.. weird.

In the picture below, the black boxes are drawn at the position where the bodies associated with the rectangular shapes should be, and the balls stuck where the "boxes" actually are.
Image

In the next picture you'll see red and black boxes. The red boxes should mark where the shapes actually are (using "love.graphics.polygon( "line", v.shape:getPoints()" ) ), and the black boxes should mark where the bodies are. Curiously the shapes and bodies should have exact the same position, but they're always drawn in two different vectors.
Image

Also, the game crashes if I'm trying to spawn a ball in the upper rim of the window.

I attached the game so you can try it yourself and maybe see what I did wrong in the code. Hopefully you can help me as soon as possible.

Thanks in advance.

PS: I aligned the window size and everything related to it to a 800x600 resolution, so everyone can try it out.

Re: Problems with the physics functions

Posted: Thu Jun 23, 2011 9:56 pm
by slime
When you call

Code: Select all

tbl.shape = love.physics.newRectangleShape( tbl.body, x, y, 100, 100 )
the x,y position of the shape is actually the offset from the body position. Change it to

Code: Select all

tbl.shape = love.physics.newRectangleShape( tbl.body, 0, 0, 100, 100 )
and your shapes should be where you want. The same goes for the circle shapes.

EDIT: That might make the body be at the top left corner of the rectangle shape, I don't remember how box2d handles that.

Re: Problems with the physics functions

Posted: Thu Jun 23, 2011 10:12 pm
by bartbes
slime wrote:EDIT: That might make the body be at the top left corner of the rectangle shape, I don't remember how box2d handles that.
Box2D uses center, as center of mass is the most important coordinate in physics.

Re: Problems with the physics functions

Posted: Thu Jun 23, 2011 11:22 pm
by RP-01
Thank you very much. That part works now.