Page 1 of 1

problem with newPolygonShape function

Posted: Sun Jul 24, 2011 3:50 am
by NC22
I dont got how to use it. It first attempt I try to create ellipse by simple math

Code: Select all

local t =0
for n = 1,8 do
local x,y =0
x=(self.width/2)*math.cos(t*math.pi)
y=(self.height/2)*math.sin(t*math.pi)
self.dots[n]={}
self.dots[n].x=self.body:getX()-x
self.dots[n].y=self.body:getY()-y
t=t+0.25
end
that gives me 8 point for create polygon (when i draw it in love.graphics.line( ) without physic it look good and all fine). But when i try to create it like physic object

Code: Select all

self.shape = love.physics.newPolygonShape(self.body ,self.dots[1].x,self.dots[1].y , self.dots[2].x,self.dots[2].y , self.dots[3].x,self.dots[3].y , self.dots[4].x,self.dots[4].y ,self.dots[5].x,self.dots[5].y , self.dots[6].x,self.dots[6].y , self.dots[7].x,self.dots[7].y , self.dots[8].x,self.dots[8].y)

i got just clear screen when draw it. Nothing displayed. Set back for example self.shape =love.physics.newRectangleShape(self.body , 0, 0, self.width , self.height) - it render normal
Draw function

Code: Select all

love.graphics.polygon("line", self.shape:getPoints())

Re: newPolygonShape

Posted: Sun Jul 24, 2011 6:58 am
by Robin
Where is the body, where is the world?

Have you read the forum rules?

Re: newPolygonShape

Posted: Sun Jul 24, 2011 7:14 am
by NC22
Robin Sorry . What rule i broke ? I try translate rules in my native language and most of them i got.
World is there

Code: Select all

worldsize = 16000
	world = love.physics.newWorld(0, 0, worldsize, worldsize)
body creates with cords for example in 1000,1000 by function

Code: Select all

self.body = love.physics.newBody(world, x, y, 10, 0)
then create shape

Re: newPolygonShape

Posted: Sun Jul 24, 2011 7:20 am
by Robin
NC22 wrote:Robin Sorry . What rule i broke ?
Provide a .love. See Game Distribution.

Re: problem with newPolygonShape function

Posted: Sun Jul 24, 2011 7:52 am
by NC22
Provide a .love
here is a .love
uncomment 39 line for look where dots of ellipse generated . But i cant see where love.graphics.polygon("line", egofizika:getPoints()) render

Re: problem with newPolygonShape function

Posted: Sun Jul 24, 2011 7:52 am
by ivan

Code: Select all

local x,y =0
This is not wrong, but y will be 'nil' unless you write:

Code: Select all

local x, y=0,0

Code: Select all

self.dots[n]={}
self.dots[n].x=self.body:getX()-x
self.dots[n].y=self.body:getY()-y
You don't need to translate your vertices.
"newPolygonShape" accepts local body coordiates.
Also, you are creating an 8-sided polygon.
Why not just create a circle shape using "newCircleShape"

Code: Select all

self.shape = love.physics.newPolygonShape(self.body ,self.dots[1].x,self.dots[1].y , self.dots[2].x,self.dots[2].y , self.dots[3].x,self.dots[3].y , self.dots[4].x,self.dots[4].y ,self.dots[5].x,self.dots[5].y , self.dots[6].x,self.dots[6].y , self.dots[7].x,self.dots[7].y , self.dots[8].x,self.dots[8].y)
There's probably a better way to code this.
Take a look at the Lua function called 'unpack'.

Re: problem with newPolygonShape function

Posted: Sun Jul 24, 2011 7:55 am
by NC22
You don't need to translate your vertices.
"newPolygonShape" accepts local body coordiates.
lol. I do similar mistake again . I cant remember how that works ) thx for advices .
newCircleShape
But its not a ellipse . It just have radius. But i try do physic of some character that have not ball form

Re: problem with newPolygonShape function

Posted: Sun Jul 24, 2011 8:07 am
by ivan
Here, I tried to simplify your code a little bit:

Code: Select all

dots={}

function love.load()
	love.graphics.setLineWidth(2)
	worldsize = 16384
	world = love.physics.newWorld(-800, -600, 800, 600)
	world:setGravity(0, 700)
	world:setMeter(64)

	nevebicheskayahernya = love.physics.newBody(world, 100, 500, 0, 0)
	eetelo = love.physics.newRectangleShape(nevebicheskayahernya , 0, 0, 500, 50)	

	for n = 1,8 do
		local n = n/8*(math.pi*2)
		local x=25*math.cos(n)
		local y=25*math.sin(n)
		table.insert(dots, x)
		table.insert(dots, y)
	end  
	elips= love.physics.newBody(world, 200, 440, 10 , 1)

	egofizika = love.physics.newPolygonShape(elips , unpack(dots))
end

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

function love.draw()
	love.graphics.setColor(255,255,255,255)
	--love.graphics.line( unpack(dots))

	love.graphics.polygon("line", eetelo:getPoints())
	love.graphics.polygon("line", egofizika:getPoints())
end
If you want to use "love.graphics.line(unpack(dots))" you have to convert the vertices back to screen coords.

Re: problem with newPolygonShape function

Posted: Sun Jul 24, 2011 8:23 am
by slime
ivan wrote:If you want to use "love.graphics.line(unpack(dots))" you have to convert the vertices back to screen coords.
That's what shape:getPoints() is for. :P

Re: problem with newPolygonShape function

Posted: Sun Jul 24, 2011 8:37 am
by ivan
Running the script again I noticed a small but gradual increase in memory usage.
Does "GetPoints" create a new Lua table each time it's called?
If it does, it might be a performance issue in some cases.