Problems with the physics functions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
RP-01
Prole
Posts: 2
Joined: Wed Jun 22, 2011 11:25 am

Problems with the physics functions

Post 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.
Attachments
bouncy balls.love
(1.85 KiB) Downloaded 151 times
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Problems with the physics functions

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problems with the physics functions

Post 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.
RP-01
Prole
Posts: 2
Joined: Wed Jun 22, 2011 11:25 am

Re: Problems with the physics functions

Post by RP-01 »

Thank you very much. That part works now.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 6 guests