physcis help

i am trying to learn codea but i dont really understand vec2 yet. i made this small test but for some reason the boxes are spassing out and i could really use some help. :slight_smile:

here is the code for my game:
i dont know why the code in the post has split up into different parts.

– vec2 test

displayMode(FULLSCREEN)

function setup()

box=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box.x=WIDTH/2
box.y=HEIGHT/2

floor=physics.body(POLYGON,vec2(0,0),vec2(0,10),vec2(1024,10),vec2(1024,0))
floor.type=STATIC
floor.x=0
floor.y=0

box2=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box2.x=412
box2.y=0

box3=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box3.x=612
box3.y=0

box4=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(50,300),vec2(300,0))
box4.x=412
box4.y=760

box5=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(50,50),vec2(50,0))
box5.x=500
box5.y=600
box5.restitution=0.7

end

function draw()

background(255, 255, 255, 255)

fill(25, 0, 255, 255)

rect(box.x,box.y,100,100)

rect(floor.x,floor.y,1024,10)

rect(box2.x,box2.y,100,100)

rect(box3.x,box3.y,100,100)

rect(box4.x,box4.y,300,50)

rect(box5.x,box5.y,50,50)

end

@iampsimon, looks like you mixed the coords of your 3rd vertex in box4. It should look like this instead:

box4=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(300,50),vec2(300,0))

@Slashin8r, thanks for the help :D, but the litte box seem to still spass out. (Sorry for my bad English)

@iampsimon, everything is working as expected, you are simply not accounting for the angle of the body when drawing it. I added a polygon function to draw your polygons, try out the code below and you can verify it is working as expected:

-- vec2 test

displayMode(FULLSCREEN)

function setup()
box=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box.x=WIDTH/2
box.y=HEIGHT/2

floor=physics.body(POLYGON,vec2(0,0),vec2(0,10),vec2(1024,10),vec2(1024,0))
floor.type=STATIC
floor.x=0
floor.y=0

box2=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box2.x=412
box2.y=0

box3=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box3.x=612
box3.y=0

box4=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(300,50),vec2(300,0))
box4.x=412
box4.y=760

box5=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(50,50),vec2(50,0))
box5.x=500
box5.y=600
box5.restitution=0.7
end

function polygon(p)
    pushMatrix()
    translate(p.x, p.y)
    rotate(p.angle)
    for i = 1, #p.points do
        local a = p.points[i]
        local b = p.points[(i % #p.points)+1]
        line(a.x, a.y, b.x, b.y)
    end
    popMatrix()
end

function draw()
background(255, 255, 255, 255)
fill(25, 0, 255, 255)
stroke(25, 0, 255, 255)
strokeWidth(2)

rect(floor.x,floor.y,1024,10)

polygon(box)
polygon(box2)
polygon(box3)
polygon(box4)
polygon(box5)
end

@Slashhin8r, thanks man. You’re the best. Thanks for taking time to answer my question and fixing my code. You just made my day.

@Slashhin8r could you please explain the polygon(p) function. What’s the stuff inside it does. You don’t have to explain it if you don’t have the time but It would really help me if you did.

The polygon function will connect the points of a polygon with lines, translate (move) the shape to the correct x,y location on the screen, and rotate the polygon according to its angle. It is a universal function for drawing any polygon you create as a wireframe representation of that polygon.

@Slashin8r, thanks man :smiley:

@Slahin8r, is there a way to draw a rectange or a sprite instead of drawing a line?

Best way to get a solid shape is to put the vertices into a mesh and then give that mesh a texture. Since you are only making rectangles, you could use rect instead of drawing the lines, but if you want the function to work for any other shape it would need to be rendered with a mesh.

For simple rectangles, try this:

function rectangle(p)
    pushMatrix()
    pushStyle()
    translate(p.x, p.y)
    rotate(p.angle)
    rectMode(CORNERS)
    rect(p.points[1].x, p.points[1].y, p.points[3].x, p.points[3].y)
    popStyle()
    popMatrix()
end

Using this function, you will need to have your 1st and 3rd vertices as opposite corners of the rectangles (which is currently how your rectangles are setup).

@Slashin8r, thanks. If I want to use a sprite instead do I make a mesh then?

@Slashin8r figured the sprite stuff out my self :smiley: thanks for all your help

Ah, nice. I was just about to post a function for it. Well, here it is anyways:

-- vec2 test

displayMode(FULLSCREEN)

function setup()
sampletexture1 = readImage("Platformer Art:Block Special")
sampletexture2 = readImage("Platformer Art:Block Grass")

box=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box.x=WIDTH/2
box.y=HEIGHT/2

floor=physics.body(POLYGON,vec2(0,0),vec2(0,10),vec2(1024,10),vec2(1024,0))
floor.type=STATIC
floor.x=0
floor.y=0

box2=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box2.x=412
box2.y=0

box3=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box3.x=612
box3.y=0

box4=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(300,50),vec2(300,0))
box4.x=412
box4.y=760

box5=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(50,50),vec2(50,0))
box5.x=500
box5.y=600
box5.restitution=0.7

shape=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(25,75),vec2(50,50),vec2(50,0))
shape.x=420
shape.y=600
shape.restitution=0.7
end

function polygon(p,texture,highX,highY)
    pushMatrix()
    translate(p.x, p.y)
    rotate(p.angle)
    local m = mesh()
    m.vertices = triangulate(p.points)
    m.texture = texture
    local coords = {}
    for i,v in ipairs(m.vertices) do
        coords[i] = vec2(v.x/highX,v.y/highY)
    end
    m.texCoords = coords
    m:draw()
    popMatrix()
end

function polygonWireframe(p)
    pushMatrix()
    translate(p.x, p.y)
    rotate(p.angle)
    for i = 1, #p.points do
        local a = p.points[i]
        local b = p.points[(i % #p.points)+1]
        line(a.x, a.y, b.x, b.y)
    end
    popMatrix()
end

function rectangle(p)
    pushMatrix()
    pushStyle()
    translate(p.x, p.y)
    rotate(p.angle)
    rectMode(CORNERS)
    rect(p.points[1].x, p.points[1].y, p.points[3].x, p.points[3].y)
    popStyle()
    popMatrix()
end

function draw()
background(255, 255, 255, 255)

pushStyle()
fill(25, 0, 255, 255)
stroke(25, 0, 255, 255)
strokeWidth(2)
rect(floor.x,floor.y,1024,10)
polygonWireframe(box)
rectangle(box2)
rectangle(box3)
rectangle(box4)
popStyle()

polygon(box5,sampletexture1,50,50)
polygon(shape,sampletexture2,50,75)
end

This will render an image texture onto any shape as long as you know the highX and highY values of the shape. I used a rectangle and 5-sided polygon for testing. The rectangle is 50x50 so highX and highY are 50, and the 5-sided shape is that same rectangle with a triangle on top that is 25 tall, so highX is 50 and highY is 75 (50 + 25 for the triangle).

@Slashin8r, thanks for the help anyways

Here is a better one that let’s you use the texture width and height or the polygon width and height. I also renamed the highX and highY variables to polyWidth and polyHeight:

-- vec2 test

displayMode(FULLSCREEN)

function setup()
sampletexture1 = readImage("Platformer Art:Block Special")
sampletexture2 = readImage("Platformer Art:Block Grass")

box=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box.x=WIDTH/2
box.y=HEIGHT/2

floor=physics.body(POLYGON,vec2(0,0),vec2(0,10),vec2(1024,10),vec2(1024,0))
floor.type=STATIC
floor.x=0
floor.y=0

box2=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box2.x=412
box2.y=0

box3=physics.body(POLYGON,vec2(0,0),vec2(0,100),vec2(100,100),vec2(100,0))
box3.x=612
box3.y=0

box4=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(300,50),vec2(300,0))
box4.x=412
box4.y=760

box5=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(50,50),vec2(50,0))
box5.x=500
box5.y=600
box5.restitution=0.7

shape=physics.body(POLYGON,vec2(0,0),vec2(0,50),vec2(25,75),vec2(50,50),vec2(50,0))
shape.x=420
shape.y=600
shape.restitution=0.7
end

function polygon(p,texture,polyWidth,polyHeight)
    pushMatrix()
    translate(p.x, p.y)
    rotate(p.angle)
    local m = mesh()
    m.vertices = triangulate(p.points)
    m.texture = texture
    local coords = {}
    local w,h
    if polyWidth ~= nil and polyHeight ~= nil then
        w,h = polyWidth,polyHeight
    else
        w,h = texture.width,texture.height
    end
    for i,v in ipairs(m.vertices) do
        coords[i] = vec2(v.x/w,v.y/h)
    end
    m.texCoords = coords
    m:draw()
    popMatrix()
end

function polygonWireframe(p)
    pushMatrix()
    translate(p.x, p.y)
    rotate(p.angle)
    for i = 1, #p.points do
        local a = p.points[i]
        local b = p.points[(i % #p.points)+1]
        line(a.x, a.y, b.x, b.y)
    end
    popMatrix()
end

function rectangle(p)
    pushMatrix()
    pushStyle()
    translate(p.x, p.y)
    rotate(p.angle)
    rectMode(CORNERS)
    rect(p.points[1].x, p.points[1].y, p.points[3].x, p.points[3].y)
    popStyle()
    popMatrix()
end

function draw()
background(255, 255, 255, 255)

pushStyle()
fill(25, 0, 255, 255)
stroke(25, 0, 255, 255)
strokeWidth(2)
rect(floor.x,floor.y,1024,10)
polygonWireframe(box)
rectangle(box2)
rectangle(box3)
popStyle()

polygon(box4,sampletexture1,300,50)
polygon(box5,sampletexture1)
polygon(shape,sampletexture2)
end

To use the texture width and height, leave polyWidth and polyHeight blank as I did for box5 and shape. To stretch the texture to fit the whole polygon, you must then fill in polyWidth and polyHeight like I did for box4.

Hope this helps. You should be able to put a texture onto any polygon no matter the shape or size using the polygon function.

@Slashin8r, thanks again. I can see that things are getting really complicated :slight_smile:

Yeah it gets more complicated once you move into using meshes, but meshes can also do much more than the standard sprite. Meshes also allow for 3D, which then gets even more complicated, lol.

@Slashin8r, i think my brain explode before I get to 3D stuff.

@Slashin8r could you explain the physics.joint? I don’t really know how to connect two bodies. :slight_smile: