Polygon shape - runtime error

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.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Polygon shape - runtime error

Post 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!
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Polygon shape - runtime error

Post 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.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Polygon shape - runtime error

Post 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.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Polygon shape - runtime error

Post by Sky Rider »

Somehow, polygon shapes do work when I create another project... Fine, I'll try to experiment with my code.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Polygon shape - runtime error

Post 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?
Last edited by Sky Rider on Mon Jun 18, 2012 3:36 pm, edited 1 time in total.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Polygon shape - runtime error

Post 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?
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Polygon shape - runtime error

Post 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.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Polygon shape - runtime error

Post 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).
Shallow indentations.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Polygon shape - runtime error

Post 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. :(
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Polygon shape - runtime error

Post by Boolsheet »

That means the rotational inertia is too low.

I would explain, but I don't understand that part of physics and Box2D.
Shallow indentations.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests