Triangle trouble

It seems weird to me that Corona SDK only supports three different shapes natively (rect, roundedrect, and circle).

Anyone know how to make a nice vector triangle without using an image file? [import]uid: 115085 topic_id: 27230 reply_id: 327230[/import]

You can draw polylines as such, but they can not be filled if I’m not mistaken:

>>

The Polylines API draws connected line segments, with any color and stroke width, using the Corona vector engine. The resulting object is a standard Corona display object, and can be independently moved and scaled like any other display object. The stroke and color properties can also be changed after the object has been drawn.

Polylines are a series of vertices specifying endpoints of line segments. The lines have a stroke width, specified in a “width” parameter. Currently, multiple line segments have miter joins. In other words, an angle joint comes to a point, as opposed to being rounded. The start and end have butt caps.

The current Polyline API is as follows:

local a = display.newLine( [parent,] x1,y1, x2,y2 )
a:setColor( r, g, b, a )
a:append( x, y [,…] )
a.width = strokeWidth

>> [import]uid: 70134 topic_id: 27230 reply_id: 110611[/import]

I believe filled polygons are on the roadmap for release soon. [import]uid: 8271 topic_id: 27230 reply_id: 110635[/import]

I’ve had zero luck trying to make a triangle from polylines. Anyone have a code snippet for a triangle? [import]uid: 115085 topic_id: 27230 reply_id: 110637[/import]

[lua]line = display.newLine(100,100,200,100)
line:append( 150,200 , 100,100 )
line.width = 10[/lua] [import]uid: 8271 topic_id: 27230 reply_id: 110638[/import]

Thanks, but like my experiments before, the actual shape of this (and how it interacts with other shapes) is not correct. The triangle looks good, but it’s real shape is different, dipping beyond my borders on one side, and having a invisible border on the other, bouncing off other objects. [import]uid: 115085 topic_id: 27230 reply_id: 110639[/import]

What do you mean by “the actual shape of this” ?

I’m going to assume that you’re referring to using the triangle in a physics environment, which is not something you’ve mentioned yet.

If so, you need to follow the guidelines for assigning shapes to physics bodies:

https://developer.anscamobile.com/content/game-edition-physics-bodies#Polygon_bodies

It’s a lot easier than it looks and there’s plenty of posts in the forums and some samples in the Corona application directory. [import]uid: 8271 topic_id: 27230 reply_id: 110640[/import]

It means “The actual shape of it” as opposed to what it looks like.

It’s behaving oddly when touching the environment around it. I will refer you back to my previous description.

The link you sent talks about assigning a shape for a graphic assets, not a vector line. [import]uid: 115085 topic_id: 27230 reply_id: 110643[/import]

Once the vector is produced it behaves as a graphic display object so the method indicated in my link is appropriate. If it wasn’t I would not have given it.

Considering that you have not mentioned physics yet can you clearly state what you meant in your previous post? [import]uid: 8271 topic_id: 27230 reply_id: 110644[/import]

This doesn’t work.

line = display.newLine(100,100,200,100)
line:append( 150,200 , 100,100 )
line.width = 2
lineShape = { 0,-35, 37,30, -37,30 }

physics.addBody( line, { density=0.6, friction=0.5, bounce=0.2 shape=lineShape } )

line:addEventListener( “touch”, dragBody )

And this doesn’t work

line = display.newLine(100,100,200,100)
line:append( 150,200 , 100,100 )
line.width = 2
lineShape = { 150,200 , 100,100 }

physics.addBody( line, { density=0.6, friction=0.5, bounce=0.2 shape=lineShape } )

line:addEventListener( “touch”, dragBody )

[import]uid: 115085 topic_id: 27230 reply_id: 110653[/import]

Have you tried adding:

[lua]physics.setDrawMode(“hybrid”)[/lua]

to the top of your code? [import]uid: 8271 topic_id: 27230 reply_id: 110658[/import]

This works:

[lua]physics = require(“physics”)
physics.start()
physics.setDrawMode(“hybrid”)

line = display.newLine(100,100,200,100)
line:append( 150,200 , 100,100 )
line.width = 2

lineShape = { 0,0 , 100,0 , 50,100 }

physics.addBody( line, { density=0.6, friction=0.5, bounce=0.2, shape=lineShape } )

function dragBody( event )
– event handling
end
line:addEventListener( “touch”, dragBody )[/lua]

I think the trap you’re falling into is that the graphical shape and the physics shape do not line up. When drawing the graphical shape you’re thinking in units on the screen, but the physics shape centres around 0,0 [import]uid: 8271 topic_id: 27230 reply_id: 110659[/import]

That worked! I can only drag it by the edges, but I hear filling the polylines is coming soon…
OK, so how would I make a triangle half the size of the current one? I reduced the numbers in half, but the result was not pretty!

Would like an example of half the size of the current example, to help me understand the dynamics of how polylines work.

Thanks! [import]uid: 115085 topic_id: 27230 reply_id: 110798[/import]

First thing: Change all the values so they work around a centre point of 0,0
This means some of the values will be negative, but that’s fine.

Once that works, the values you use to create the display object will be the same as the values for the physics shape. You should be able to create a for loop which fills in the display group and pass the same table of points for the physics shape. Give that a good, honest try - but if you really can’t do it, post back here and I’ll show you how.

This does take a bit of mental leaping, but it’s something that all game/graphic developers fight with and you will get it. Just takes practice.

Once you have the points all centred around 0,0 making the shape half it’s normal size actually is about dividing everything by 2. The solution above won’t allow that because the physics shape is centred on a different point than the display shape.

One trick I use is to create all display objects in their own display group, so everything for each shape is actually centred on 0,0. The physics shape is then applied to the group. This doesn’t really help you here, because of the simple shape you’re using (it’s not more than one image) but it can help when thinking about the problem. [import]uid: 8271 topic_id: 27230 reply_id: 110803[/import]