Howdy, Stranger!

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

Trouble implanting complex polygonal physics bodies

edited September 2014 in Bugs Posts: 63

So I wanted to make a physics body that could catch other objects. It would be like a rectangle with a missing upside down trapezoid shape with a height that is 3/4th the height of the rectangle itself. The reason why I am pointing this out is because I had a rectangle shape assigned to it to make the body visible so that you would just see a regular rectangle rather than the objects actual shape. Also take into account that the floors position and the objects size are proportional to the size of the screen.

Here are the problems that I encountered when I ran my code:
1. The object is vertical. This might be because the order in which I entered the corners positions is wrong as I originally thought that you started by entering the top left corner and then you went clockwise from there.
2. The object stops before going near the floor. I honestly would not have the slightest idea as to why this is happening. I positioned the floors y coordinates at HEIGHT / 10 (there is a line to indicate its location) yet the object seems to stop falling at around HEIGHT / 5.
3. This is an animation bug that might be something that only I am experiencing where the rectangle image does not disappear from where the physics body PREVIOUSLY WAS. So I'm left with an annoying trail of rectangles following the actual body.

Here is the code:


-- Minecart Prototype -- Use this function to perform your initial setup function setup() print("Hello World!") end -- This function gets called once every frame function draw() -- This sets a dark background color -- This sets the line thickness strokeWidth(5) -- Do your drawing here pushStyle() fill(255, 0, 20, 255) line(0,HEIGHT / 10, WIDTH, HEIGHT / 10) rect(minecart.x, minecart.y, minecart_width, minecart_height) popStyle() end w1 = physics.body(EDGE,vec2(0,HEIGHT / 10),vec2(WIDTH,HEIGHT/10)) w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT)) w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT)) minecart_width = WIDTH / 10 minecart_height = HEIGHT / 8 minecart = physics.body(POLYGON, vec2(-minecart_width/2,minecart_height/2), vec2(-minecart_width/4,-minecart_height/4), vec2(minecart_width/4,-minecart_height/4), vec2(minecart_width/2,minecart_height/2), vec2(minecart_width/2,-minecart_height/2), vec2(-minecart_width/2,-minecart_height/2)) minecart.gravityScale = 1 minecart.x = WIDTH/2 minecart.y = HEIGHT/2

Comments

  • dave1707dave1707 Mod
    Posts: 7,753

    @Paintcannon Are you trying to do this.


    function setup() physics.continuous=true b=physics.body(CHAIN,false,vec2(-20,20),vec2(-10,0),vec2(10,0),vec2(20,20)) b.x=300 b.y=300 c=physics.body(CIRCLE,10) c.x=310 c.y=600 end function draw() background(40, 40, 50) stroke(255) strokeWidth(2) line(b.x-20,b.y+20,b.x-10,b.y) line(b.x-10,b.y,b.x+10,b.y) line(b.x+10,b.y,b.x+20,b.y+20) fill(255) ellipse(c.x,c.y,20) end
  • dave1707dave1707 Mod
    edited September 2014 Posts: 7,753

    @Paintcannon When you post code, put 3 ~'s on a line before and after you code so it shows correctly in the forum. I added the 3 ~’s to your code above.

  • dave1707dave1707 Mod
    Posts: 7,753

    @Paintcannon I rearranged your code. I added background(40,40,50) to stop multiple images and added rectmode(CENTER) so the rectangle hit the floor.


    -- Minecart Prototype function setup() rectMode(CENTER) w1 = physics.body(EDGE,vec2(0,HEIGHT / 10),vec2(WIDTH,HEIGHT/10)) w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT)) w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT)) minecart_width = WIDTH / 10 minecart_height = HEIGHT / 8 minecart = physics.body(POLYGON, vec2(-minecart_width/2,minecart_height/2), vec2(-minecart_width/4,-minecart_height/4), vec2(minecart_width/4,-minecart_height/4), vec2(minecart_width/2,minecart_height/2), vec2(minecart_width/2,-minecart_height/2), vec2(-minecart_width/2,-minecart_height/2)) minecart.gravityScale = 1 minecart.x = WIDTH/2 minecart.y = HEIGHT/2 end function draw() background(40,40,50) strokeWidth(5) pushStyle() fill(255, 0, 20, 255) line(0,HEIGHT / 10, WIDTH, HEIGHT / 10) rect(minecart.x, minecart.y, minecart_width, minecart_height) popStyle() end
  • edited September 2014 Posts: 63

    Yes thank you this works perfectly now

  • And what you have there is basically what I am trying to make only with objects that fall from random locations and a cart with tilt based movement that can catch them

  • dave1707dave1707 Mod
    edited September 2014 Posts: 7,753

    @Paintcannon Here's a start. I posted something similar to this a long time ago for someone else.

    EDIT: tilt the ipad to move the cart.


    displayMode(FULLSCREEN) function setup() physics.continuous=true size=50 b=physics.body(CHAIN,false,vec2(-size,size),vec2(-size/2,0), vec2(size/2,0),vec2(size,size)) b.x=300 b.y=100 b.type=KINEMATIC tab={} count=0 create() end function create() c=physics.body(CIRCLE,10) c.x=math.random(50,WIDTH-50) c.y=HEIGHT table.insert(tab,c) end function draw() background(40, 40, 50) stroke(255) strokeWidth(2) line(b.x-size,b.y+size,b.x-size/2,b.y) line(b.x-size/2,b.y,b.x+size/2,b.y) line(b.x+size/2,b.y,b.x+size,b.y+size) fill(255) for a,b in pairs(tab) do ellipse(b.x,b.y,20) if b.y<0 then b:destroy() table.remove(tab,a) end end b.linearVelocity=vec2(Gravity.x*1000,0) count=count+1 if count>140 then count=0 create() end end
  • This is pretty much what I have been working towards only in this case I do not understand how the ball is created CONTINUOUSLY and how/where it is assigned gravity. I also do not know how the Boolean: physics.continous affects this game.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Paintcannon

    have a look in the built in reference for what the continuous property does

    Gravity is assigned automatically, but you can alter the settings. If you look at the physics section of the reference, you will see them.

  • dave1707dave1707 Mod
    Posts: 7,753

    @Paintcannon The ball isn't created continuously. It's created every 140 draw cycles using the "if count" statement. draw() runs about 60 times per second and count is incremented each time draw() runs. So a ball will be created every 140/60 seconds or a little over every 2 seconds.

Sign In or Register to comment.