Page 1 of 2
Polygon shape - runtime error
Posted: Mon Jun 18, 2012 9:25 am
by Sky Rider
Hey everyone!
I've been working on something like spaceship TDS. So, to test Box2D engine, I first used circle shapes for my spaceships, but then I thought it would be kinda cool to use polygons to make it look more realistic. I made a shape with 8 vertices, and love crashed with a runtime error! Then I discovered that even such a simple polygon as
Code: Select all
love.physics.newPolygonShape(0, 0, 0, 64, 64, 64, 64, 0) --square polygon, anti-clockwise
doesn't work. Microsoft Visual C++ cryes that "This application has requested the Runtime to terminate it in an unusual way". I tryed to install the last MVC++ 2010, then I updated my video drivers (my video card is ATI Mobility Radeon HD 3470 and OS is Windows Vista, by the way). Nothing helped

Could anyone help me? Thanks in advance!
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 10:40 am
by OmarShehata
Does the creation of other bodies work normally? Only thing I can think of to get that one line not to work is that you might be calling it before setting up your world.
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 12:32 pm
by Sky Rider
I havent tested all the bodies, but circle and rectangle works pretty good. Thats strange since a rectangle, according to wiki, is just a type of polygon.
And the problem is certainly not in the code: when I replace a polygon with a circle or a rectangle, it does work. I think it's something with my computer, maybe some drivers missing... I'll try to update my staff.
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 3:05 pm
by Sky Rider
Somehow, polygon shapes do work when I create another project... Fine, I'll try to experiment with my code.
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 3:30 pm
by Sky Rider
I've solved the problem by moving the chunk of code that determines mass, inertia and position of the body. Now my code looks like this:
Code: Select all
self.body = love.physics.newBody(world, x, y, "dynamic")
self.body:setMass(25)
self.body:setInertia(25)
self.body:setPosition(x, y)
self.shape = love.physics.newPolygonShape(0, 0, 71, 0, 95, 11, 102, 22, 102, 60, 95, 71, 71, 82, 0, 82)
self.fixture = love.physics.newFixture(self.body, self.shape)
self.fixture:setRestitution(0.05)
However, now I have another problem: changing mass doesn't seem to work. There's no difference between a mass of 25 and 2500... The force rotates and moves it with the same effect. Why could that happen?
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 3:35 pm
by OmarShehata
Sky Rider wrote:I've solved the problem by moving the chunk of code that determines mass, inertia and position of the body. Now my code looks like this:
Code: Select all
self.body = love.physics.newBody(world, x, y, "dynamic")
self.body:setMass(25)
self.body:setInertia(25)
self.body:setPosition(x, y)
self.shape = love.physics.newPolygonShape(0, 0, 71, 0, 95, 11, 102, 22, 102, 60, 95, 71, 71, 71, 0, 71)
self.fixture = love.physics.newFixture(self.body, self.shape)
self.fixture:setRestitution(0.05)
However, now I have another problem: changing mass doesn't seem to work. There's no difference between a mass of 25 and 2500... The force rotates and moves it with the same effect. Why could that happen?
More info please. Where is the code where you apply the force? Where exactly do you apply a force?
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 3:51 pm
by Sky Rider
I apply the force using controls:
Code: Select all
function math.lengthdir(len, dir)
return len * math.cos(dir), len * math.sin(dir)
end
function hostPlayer:checkControls(dt)
if love.keyboard.isDown('w') then
self.body:applyForce(math.lengthdir(1000, self.body:getAngle()))
elseif love.keyboard.isDown('s') then
self.body:applyForce(math.lengthdir(1000, self.body:getAngle() + math.pi))
end
if love.keyboard.isDown('d') then
self.body:applyAngularImpulse(100)
elseif love.keyboard.isDown('a') then
self.body:applyAngularImpulse(-100)
end
end
It happens every timestep.
Here's all code that uses physics:
Code: Select all
function love.load()
world = love.physics.newWorld(0, 0, true)
love.physics.setMeter(64)
hostPlayer:create(0, 0, "EaJet", 0, 0, 0, 0.5) -- this function calls physical:init (a parent class) with the same arguments.
end
function physical:init(x, y, image, speed, dir, angle, imageScale)
table.insert(objList, self)
self.id = table.maxn(objList)
self.image = love.graphics.newImage(image .. ".png")
self.imageWidth = self.image:getWidth()
self.imageHeight = self.image:getHeight()
if imageScale == nil then
self.imageScale = 1
else
self.imageScale = imageScale
end
self.body = love.physics.newBody(world, x, y, "dynamic")
self.body:setMass(25)
self.body:setInertia(25)
self.body:setPosition(x, y)
self.shape = love.physics.newPolygonShape(0, 0, 71, 0, 95, 11, 102, 22, 102, 60, 95, 71, 71, 82, 0, 82)
self.fixture = love.physics.newFixture(self.body, self.shape)
self.fixture:setRestitution(0.05)
if angle == nil then
self.body:setAngle(0)
else
self.body:setAngle(math.rad(angle))
end
if speed ~= nil then
self.body:setLinearVelocity(math.lengthdir(speed, dir))
end
end
--Then hostPlayer:checkControls(dt) is called every frame.
Basically, there's nothing else that uses physics.
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 3:59 pm
by Boolsheet
Box2D recalculates the mass for the body if you attach a new fixture. If you want to change the mass, do it after the attaching.
Also, I recommend using
Body:setMassData because
Body:setMass doesn't catch exceptions in 0.8.0 (which is probably why it terminated for you).
Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 4:13 pm
by Sky Rider
Thanks!
But for some reason an error occures:
"Box2D error: m_l > 0.0f"
And it doesn't matter whether I put "setMassData" before fixture or after it.

Re: Polygon shape - runtime error
Posted: Mon Jun 18, 2012 4:17 pm
by Boolsheet
That means the rotational inertia is too low.
I would explain, but I don't understand that part of physics and Box2D.