It looks like you're new here. If you want to get involved, click one of these buttons!

Hey Guys,

I'm using the Physics Test example as a model for a building block game I'm building. I just have a basic question that I can't figure out how to do. How do I color the physics box that is in test 1? Sorry if this is a noob question but I could only manage to color the circle.

Please help.

Tagged:

## Comments

Everything is drawn in PhysicsDebugDraw, look out for body.shapetype == POLYGON.

If you do not want to interfere with the 'overlay' provided by â€˜PhysicsDebugDrawâ€˜, then how about something like:

Hey @mpilgrem i tried your code in the physics example and it works perfectly, just not in my game. i keep on getting errors. here's the basic code im using for the box. it was in one of @Reefwing's tutorials. Hopefully he can help http://codeatuts.blogspot.com/2012/07/tutorial-11-physics-101.html

if anyone can tell me how to do it directly from PhysicsDebugDraw, it would be really helpful. I tried putting

where i managed to adjust the stroke but i got no luck

Come on, you're using

fillto adjust thestroke, even the English language reveals your bug.Read the code in PhysicsDebugDraw carefully, what's the difference between a circle and a box in this code?

@Codeslinger oh darn it. That's my fault. I meant that's used fill to adjust the fill and got no luck. I don't know why I wrote that here. I think my problem has to do with the noStroke() before everything in PhysicsDebugDraw(). I'm still a little confused

.@veeeralp - have a look at the PhysicsDebugDraw draw() function in particular at the body.shapeType == POLYGON block. This is where squares get drawn (as they are a type of polygon). Note that the line() function (not rect) is used to draw the square, hence fill has no effect (except on the CIRCLE shapeType).

So you need to either come up with a new body.shapeType (called RECTANGLE say) and use rect() to draw it or you could use a sprite, but that is probably not the best option in this case.

.@Reefwing ok thanks. I will try it this afternoon. When I make the body.shapeType == RECTANGLE, do I have to get rid of the createBox() function. Will I have to dod everything I did with the Box in the rectangle. ie. make 1 rectangle fall every second

Maybe I have a reputation as a grumpy teacher, but I wanted veeeralp to find out the difference between a circle and a box himself, so I gave him only a hint. He wants to be a programmer, right?

veeeralp, you cannot create a new shape type named RECTANGLE, you have to live with what the physics API provides, so stick to a polygon.

## Solution idea 1

When creating a box, you have to remember that this specific kind of polygon is in fact a rectangle. When drawing a polygon, check if it is a rectangle and draw a Codea rectangle which can be filled.

## Solution idea 2

Instead of drawing the outlines of the polygon, triangulate it and then draw the triangles with a mesh. This is more difficult, but I think there are examples for this in the forum. This solution is also more universal.

.@Codeslinger is of course correct (even if he is grumpy ). I forgot that POLYGON and the like are defined types in physics body.

@Reefwing @Codeslinger I think the easiest method for me right now is @mpilgrem's code. I got it working inside the Physics Test example, just can't get it working through the Physics 101 tutorial.

The code below is a coloured-in version of

`Test1`

(polygons only):That is an elegant solution @mpilgrem.

Thinking about this, it is not actually that straight forward to work out if a polygon is a rectangle. My initial thought was, "easy just check that the number of points in the physics body was equal to 4". But of course you also need to check whether all of the internal angles are 90 degrees. If the rectangle had it sides parallel to the axis then you could just check that the (x,y) co-ordinates for the vertices lined up, but what about if it was rotated?

So what would be the easiest test for detecting whether a polygon is a rectangle? My thoughts would be to firstly check the number of vertices. If this is 4 then you can move onto the next step, because at this stage you don't even know if you have a quadrilateral as it depends on what order the lines are drawn. We know that the diagonals of a rectangle are equal in size and bisect each other and that the length of a diagonal is given by:

d = SQRT(w^2 + h^2)

Consequently we could calculate both diagonals and check that they are the same, if so the four vertices of our polygon are a rectangle. Or are they?

I'm afraid not, they may be, but to be certain you need to test whether a polygon is simple or complex. A complex polygon has intersections with itself. If our four sided polygon has equal diagonals and is simple then it is a rectangle.

Maybe it is just easier to check the internal angles? Or perhaps our resident mathematician @Andrew_Stacy may have another approach?

@Reefwing do you know how I would get it working using the coding from your physics 101 tutorial. I need to color the boxes dropping down. I always get an error

How about:

(Update) Not all simple quadrilaterals with equal-length diagonals are rectangles.

.@mpilgrem - Nice, so you are taking the dot product of two adjacent vectors which I think is equivalent to the length of the two vectors multiplied by the cosine of the internal angle and since cos 90 = 0, if the dot product is not zero, we don't have a rectangle? I'm assuming you test for > 0.0001 to take into account rounding errors?

Can you give me an example of a simple quadrilateral with two equal length diagonals which isn't a rectangle. I'm sure you are right I just cant think of an example (in 2D).

.@veeeralp - you are better off adapting @mpilgrem's code above to drop random boxes than trying to shoehorn it into my tute. Have a go and then post the code if you get stuck.

For @Reefwing (updated: less code, same effect)

.@Reefwing I'm just having trouble with error. Even when I get rid of everything that has to with your tutorial... I still get when I put self:createBox()

.@mpilgrem - Quod erat faciendum. Hypnotic!

.@veeeralp it is a bit of a hack but you need to blend the above with the tute code. Try this:

In Main for the Physics 101 tute add:

Then in the Physics tab add a new function based on @mpilgrem's code above

Finally, in the PhysicsDebugDraw:draw() function, at the very end add:

.@Reefwing thank you!!! This worked with no problems except when I tried to clear all the box pes after they reacher a certain limit. I always get an error. Any other way to clear the rectangles now

.@veeeralp - c'mon dude you have to do something yourself. You know where the problem is and you know what you just added. The answer should be obvious. Post the answer here when you work it out.

Just to say that I'd've gone for the dot products too.

A quadrilateral with equal length diagonals is a rectangle if and only if the diagonals intersect at their midpoints (note that they needn't intersect at all).

Suddenly, I start to like my self-proclaimed grumpy teacher attitude. (In response to Reefwing, not Andrew.)

.@Codeslinger - lol.

.@Andrew_Stacey - I dont think I would ever have come up with that approach, I think I would have either converted the cartesian co-ordinates to polar and checked the angle or used one of the available fill polygon algorithms (in which case you don't care if it is a rectangle).

@Reefwing I tried implementing the PhysicsDebugDraw:clear() function when the box count reaches 10, but I get a bad argument on the

part. I also tried putting resetMatrix() and got another error. Next I tried putting table.remove(polys, poly) instead of table.insert(polys, poly)

.@veeeralp - you are on the right track. Try just polys = {} in that function.

.@Reefwing thank you it worked. Now im done with the paid version of my game. Now on to the free version and then iphone

edit: do you remember that little bug that you had with the mines falling down in your minesweeper game becuase of the repeated call to the touch handler? I have the same problem right now, but worse becuase I drew the ground. How did you fix that?

@Reefwing Dot products are "angles made safe". They contain all the same information but have much better properties. If you find yourself computing angles, chances are you could do it more easily with dot products.

.@Andrew_Stacey - I love learning a new trick. Dot products it is - although if you just want the angle I guess you need to change them into unit vectors.

For completeness, it may be worth mentioning the (signed)

`angleBetween`

function built into the`vec2`

userdata type:And also important to note that it returns an angle in radians but the rotate function takes degrees.

Ouch! Makes me think of that mars rover that got lost due to metric and non-metric engineering teams... :-S

Looks like we're doing rocketscience after all.