Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with polygon based physic body position
Started by rustam Nov 16 2013 06:26 AM

35 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

rustam

[GLOBAL: userInfoPane.html]
rustam
  • Observer

  • 16 posts
  • Corona SDK

Hi all!

i'm trying create a body based on polygon.

 

-- main.lua

display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")

physics.start()
physics.setGravity(0, 10)
physics.setDrawMode("hybrid")

display.setDefault( "anchorX", 0 )
display.setDefault( "anchorY", 0 )

local vertices = { 0,0,200,0,250,50,200,100,100,150,0,100 }

local o = display.newPolygon( 0, 300, vertices )
o.x=0
o.y=300  
o.strokeWidth = 2
o:setStrokeColor( 0, 255, 255 )
 
physics.addBody(o, "static", {friction=0.0, bounce=0.4})
 

but i see wrong physic body starting from the center of polygon.

0cba051886362be79e1336895c820662c54439b2

 

and the second issue is polygon has wrong Y coordinate.

i'm using last public release 2013.2076.

 



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

This looks like a bug.  Can you file a bug report using the "Report a bug" link above.  The code above will suffice as the demo project needed.  This way the engineer's can take a look at it.

 

Thanks

Rob



[TOPIC: post.html]
#3

rustam

[GLOBAL: userInfoPane.html]
rustam
  • Observer

  • 16 posts
  • Corona SDK

Thank you Rob. I reported.



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

Can you post the bug ID back here for later reference.

 

Thanks

Rob



[TOPIC: post.html]
#5

rustam

[GLOBAL: userInfoPane.html]
rustam
  • Observer

  • 16 posts
  • Corona SDK

where can i find my bug ID?



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

It should have been in the email you got after filing it.  It would have also been on the last screen after submitting the bug I think.

 

Its 28033 for reference. 

 

Thanks

Rob



[TOPIC: post.html]
#7

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Confirm that it is a serious mistake. Even if in the example with a star, turn on physics, it will be seen that it does not correspond to objects. Please correct.

Attached File  bug_polygon.png   3.71KB   19 downloads



[TOPIC: post.html]
#8

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

And another bug: WARNING: Polygon could not be generated. The polygon outline is invalid, possibly due to holes or self-intersection.
 
Should be created because the physical form is created.


[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

@GamingStudio17 can you produce an example that creates this problem?

 

Thanks

Rob



[TOPIC: post.html]
#10

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Yeah, that's a standard example with the addition of physics http://docs.coronalabs.com/api/library/display/newPolygon.html

 

local physics = require("physics")

physics.start()
physics.setDrawMode("hybrid")

local halfW = display.contentWidth*0.5;
local halfH = display.contentHeight*0.5;

local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, }

local o = display.newPolygon( halfW, halfH, vertices )
physics.addBody(o, "static", { density = 0.1, friction = 0.8, bounce = 0} )
o.strokeWidth = 10
o:setStrokeColor( 0, 255, 255 )

 

Thanks

Anton



[TOPIC: post.html]
#11

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

What version of Corona SDK are you using?  This does not seem to be a problem in 2087.

 

Rob



[TOPIC: post.html]
#12

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Corona SDK version 2088, the same mistake about whom wrote rustam.

 

You can use the code

display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")

physics.start()
physics.setGravity(0, 10)
physics.setDrawMode("hybrid")

display.setDefault( "anchorX", 0 )
display.setDefault( "anchorY", 0 )

local vertices = { 0,0,200,0,250,50,200,100,100,150,0,100 }

local o = display.newPolygon( 0, 300, vertices )
o.x=0
o.y=300  
o.strokeWidth = 2
o:setStrokeColor( 0, 255, 255 )
 
physics.addBody(o, "static", {friction=0.0, bounce=0.4})


[TOPIC: post.html]
#13

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

There is a filed bug report on it. 

 

Rob



[TOPIC: post.html]
#14

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Thank you for a fix, but not until the end. Object still moves if changing its shape. Object must stand still.

 

here is the code demonstrates the bug:

display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")

physics.start()
physics.setGravity(0, 10)
physics.setDrawMode("hybrid")

display.setDefault( "anchorX", 0 )
display.setDefault( "anchorY", 0 )

local vertices = { 0,0,200,0,250,50,200,100,100,150,0,100 }

local o = display.newPolygon( 0, 300, vertices )
o.x=100
o.y=300
physics.addBody(o, "static", {friction=0.0, bounce=0.4})


-- touch polygon
local function touch_polygon(e)
		if(e.phase == "moved") then
            display.remove(o)
            local xx = e.x-o.x
            local yy = e.y-o.y

            local vertices = { 0,0,xx,yy,250,50,200,100,100,150,0,100 }
            o = display.newPolygon( 0, 300, vertices )
			o.x=100
			o.y=300
			physics.addBody(o, "static", {friction=0.0, bounce=0.4})
        end
end

Runtime:addEventListener("touch", touch_polygon)

 

Attached File  Снимок экрана 2013-11-26 в 19.11.47.png   8.73KB   27 downloads  Attached File  Снимок экрана 2013-11-26 в 19.11.57.png   9.7KB   27 downloads



[TOPIC: post.html]
#15

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

I don't know that this is a bug.  As you resize the polygon, the bounding box of the polygon changes because it's center mass has changed.  And of course if you drag it enough, eventually the vertices are not in clockwise order at which points the results are undefined. 

 

Rob



[TOPIC: post.html]
#16

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

There was also a check in for 2090 regarding shapes and centering, but I don't think it changes this behavior, which seems to be correct.

 

Rob



[TOPIC: post.html]
#17

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Need to add the ability to disable re-centered about the center of the polygon. Because of this, it is impossible to draw a polygon. Because I do not know where is the center of the object. Without the auto centering, center can assign the first point and then will add a polygon in the right place.



[TOPIC: post.html]
#18

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

What are you trying to accomplish? 



[TOPIC: post.html]
#19

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

I'm trying to draw a polygon in real time on the points. From the fact that I do not know where the center, I can not properly draw a polygon.
Here is an example of what I want to do. Set points and closes the figure and it is drawn.
 

display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")

physics.start()
physics.setDrawMode("hybrid")


local n_point = 0
local tohk = {}

function ptInside(obj, x, y)
        local inside = false
        local objx = obj.x - (obj.width / 2)
        local objy = obj.y - (obj.height / 2)
        if (x >= objx) and
                (x <= (objx + obj.width)) and
                (y >= objy) and
                (y <= (objy + obj.height)) then
                inside = true
        end
        return inside
end

-- Touch
local function onTouch(e)
        if(e.phase == "began") then
            startX = e.x
            startY = e.y


            bloc_add = display.newLine( startX,startY, startX,startY )
            
            if n_point ~= 0 then
                startX = save_x
                startY = save_y
            end

            tohk[n_point] = display.newCircle( startX, startY, 10 )
            tohk[n_point]:setFillColor(255,0,0)


        elseif(e.phase == "moved") then
            display.remove(bloc_add)
            bloc_add = display.newLine( startX,startY, e.x,e.y )
            bloc_add.strokeWidth = 3

            if ptInside(tohk[0], e.x, e.y) then
                tohk[0]:setFillColor(0,255,0)
            else
                tohk[0]:setFillColor(255,0,0)
            end

        elseif(e.phase == "ended") then
            bloc_add = display.newLine( startX,startY, e.x,e.y )
            bloc_add.strokeWidth = 3

            
            save_x = e.x
            save_y = e.y

            n_point = n_point + 1

            if ptInside(tohk[0], e.x, e.y) then
                local vertices = {}
                local n = 1
                for i=0,n_point-1 do

                    vertices[n] = tohk[i].x-tohk[0].x
                    print(n,vertices[n].." i "..tohk[i].x)
                    n = n + 1
                    vertices[n] = tohk[i].y-tohk[0].y
                    vertices[2] = -90
                    print(n,vertices[n])
                    n = n + 1
                    
                end

                local new_x = tohk[0].x
                local new_y = tohk[0].y



                o = display.newPolygon( new_x, new_y, vertices )
                physics.addBody(o, "static", {friction=0.0, bounce=0.4})
            end
        end

end
Runtime:addEventListener("touch", onTouch)


[TOPIC: post.html]
#20

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

Hi @GamingStudio17,

I'm jumping into this thread a bit late, so let me get caught up. You basically want to draw a polygon in real time, and then accurately trace a physics body around it? But since it's unknown where the "center" of that object will be, you can't build the body correctly?

 

By "real time", do you mean, the physics body must be constantly updated (effectively removed and re-created) on each phase of movement? Or can the application of the body occur after the user "finalizes" the drawing of the polygon?

 

Brent



[TOPIC: post.html]
#21

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Brentafter the user "finalizes" the drawing of the polygon.



[TOPIC: post.html]
#22

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

Brent correct me if I'm wrong, but physics polygon shapes have to be convex.  So you would need to make sure all your points are convex or you would have to have multiple physics bodies.  If you have a list of your vertices, you should be able to create a physics shape that works out for you instead of depending on physics to auto-shape it.

 

Rob



[TOPIC: post.html]
#23

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Well, how to make that a polygon created correctly?



[TOPIC: post.html]
#24

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

I haven't run this actual code, but @horacebury (Corona utility guru :) ) has created some modules which may do exactly what you need:

 

http://developer.coronalabs.com/code/shape-creation-use-addbody

 

http://developer.coronalabs.com/code/simple-polygon-triangulation

 

And yes, custom physics shapes must be convex at all point bends, no exceptions.

Brent



[TOPIC: post.html]
#25

GamingStudio17

[GLOBAL: userInfoPane.html]
GamingStudio17
  • Contributor

  • 163 posts
  • Corona SDK

Brentit will not work with polygon because I did not know where his center. This will only work with a shape property. Shape of the object relative to the center does not shift automatically as opposed to the type of object poligon.

 
The only thing you can do is to disable the automatic alignment of form object type poligon, or how to calculate the coordinates of the center of that object.
 
Thanks



[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]