problem with newPolygonShape function

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
NC22
Prole
Posts: 9
Joined: Sat Jul 16, 2011 12:36 am

problem with newPolygonShape function

Post by NC22 » Sun Jul 24, 2011 3:50 am

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())
Last edited by NC22 on Sun Jul 24, 2011 7:17 am, edited 1 time in total.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: newPolygonShape

Post by Robin » Sun Jul 24, 2011 6:58 am

Where is the body, where is the world?

Have you read the forum rules?
Help us help you: attach a .love.

NC22
Prole
Posts: 9
Joined: Sat Jul 16, 2011 12:36 am

Re: newPolygonShape

Post by NC22 » Sun Jul 24, 2011 7:14 am

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

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: newPolygonShape

Post by Robin » Sun Jul 24, 2011 7:20 am

NC22 wrote:Robin Sorry . What rule i broke ?
Provide a .love. See Game Distribution.
Help us help you: attach a .love.

NC22
Prole
Posts: 9
Joined: Sat Jul 16, 2011 12:36 am

Re: problem with newPolygonShape function

Post by NC22 » Sun Jul 24, 2011 7:52 am

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
Attachments
ppc.love
(706 Bytes) Downloaded 67 times
Last edited by NC22 on Sun Jul 24, 2011 7:52 am, edited 1 time in total.

User avatar
ivan
Party member
Posts: 1556
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: problem with newPolygonShape function

Post by ivan » Sun Jul 24, 2011 7:52 am

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'.

NC22
Prole
Posts: 9
Joined: Sat Jul 16, 2011 12:36 am

Re: problem with newPolygonShape function

Post by NC22 » Sun Jul 24, 2011 7:55 am

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

User avatar
ivan
Party member
Posts: 1556
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: problem with newPolygonShape function

Post by ivan » Sun Jul 24, 2011 8:07 am

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.

User avatar
slime
Solid Snayke
Posts: 2883
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: problem with newPolygonShape function

Post by slime » Sun Jul 24, 2011 8:23 am

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

User avatar
ivan
Party member
Posts: 1556
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: problem with newPolygonShape function

Post by ivan » Sun Jul 24, 2011 8:37 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests