Rectangles

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Scriptoar
Prole
Posts: 13
Joined: Sun May 08, 2011 8:49 am

Rectangles

Post by Scriptoar »

I have one rectangle as the floor in my place. When I make another, your guy can go straight through it.. Why?
:ehem: :ehem:
moar tacos = better life

u liek?
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Rectangles

Post by Jasoco »

We're gonna need your code, bro.
Scriptoar
Prole
Posts: 13
Joined: Sun May 08, 2011 8:49 am

Re: Rectangles

Post by Scriptoar »

Code: Select all

function love.load()
World = love.physics.newWorld(-8000, -8000, 8000, 8000)
World:setGravity(3, 300)
World:setMeter(64)
Bodies = {}
Shapes = {}
Bodies[0] = love.physics.newBody(World, 900/2.2, 625, 0, 0)
Shapes[0] = love.physics.newRectangleShape(Bodies[0], 0, 0, 750, 50, 0)
Bodies[1] = love.physics.newBody(World, 650/2, 650/2, 15, 0)
Shapes[1] = love.physics.newCircleShape(Bodies[1], 0, 0, 40)
Shapes[2] = love.physics.newCircleShape(Bodies[1], 0, 0, 5)
Shapes[3] = love.physics.newCircleShape(Bodies[1], 0, 0, 5)
Shapes[4] = love.physics.newRectangleShape(Bodies[0], 0, 0, 750, 50, 0)
love.graphics.setBackgroundColor(104, 136, 248)
love.graphics.setMode(1280, 1048, false, true, 0)
end

function love.update(dt)
World:update(dt)
if love.keyboard.isDown("right") then
Bodies[1]:applyForce(400, 0)
elseif love.keyboard.isDown("left") then
Bodies[1]:applyForce(-400, 0)
elseif love.keyboard.isDown("up") then
Bodies[1]:getPosition()
xPos, yPos = Bodies[1]:getPosition(x, y)
Bodies[1]:setPosition(xPos, yPos - 10)
elseif love.keyboard.isDown("escape") then
love.event.push('q')
end
end

function love.draw()
local x1, y1, x2, y2, x3, y3, x4, y4 = Shapes[0]:getBoundingBox()
local boxwidth = x3 - x2 
local boxheight = y2 - y1 
love.graphics.setColor(72, 160, 14) 
love.graphics.rectangle("fill", Bodies[0]:getX() - boxwidth/2, Bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
love.graphics.setColor(123, 77, 64)
love.graphics.circle("fill", Bodies[1]:getX(), Bodies[1]:getY(), Shapes[1]:getRadius(), 20)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("fill", Bodies[1]:getX(), Bodies[1]:getY() + boxheight/4, Shapes[2]:getRadius(), 20)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("fill", Bodies[1]:getX() + boxwidth/25, Bodies[1]:getY() + boxheight/4, Shapes[3]:getRadius(), 20)
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", Bodies[0]:getX() - boxwidth/44, Bodies[0]:getY() - boxheight/8, boxwidth, boxheight)
end
Sorry about the length.
moar tacos = better life

u liek?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Rectangles

Post by Robin »

Forum rules wrote:Before you make a thread asking for help, read this.
It would be nice if you did. :)
Help us help you: attach a .love.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Rectangles

Post by tentus »

Scriptoar wrote:

Code: Select all

function love.load()
World = love.physics.newWorld(-8000, -8000, 8000, 8000)
World:setGravity(3, 300)
World:setMeter(64)
Bodies = {}
Shapes = {}
Bodies[0] = love.physics.newBody(World, 900/2.2, 625, 0, 0)
Shapes[0] = love.physics.newRectangleShape(Bodies[0], 0, 0, 750, 50, 0)
Bodies[1] = love.physics.newBody(World, 650/2, 650/2, 15, 0)
Shapes[1] = love.physics.newCircleShape(Bodies[1], 0, 0, 40)
Shapes[2] = love.physics.newCircleShape(Bodies[1], 0, 0, 5)
Shapes[3] = love.physics.newCircleShape(Bodies[1], 0, 0, 5)
Shapes[4] = love.physics.newRectangleShape(Bodies[0], 0, 0, 750, 50, 0)
love.graphics.setBackgroundColor(104, 136, 248)
love.graphics.setMode(1280, 1048, false, true, 0)
end

function love.update(dt)
World:update(dt)
if love.keyboard.isDown("right") then
Bodies[1]:applyForce(400, 0)
elseif love.keyboard.isDown("left") then
Bodies[1]:applyForce(-400, 0)
elseif love.keyboard.isDown("up") then
Bodies[1]:getPosition()
xPos, yPos = Bodies[1]:getPosition(x, y)
Bodies[1]:setPosition(xPos, yPos - 10)
elseif love.keyboard.isDown("escape") then
love.event.push('q')
end
end

function love.draw()
local x1, y1, x2, y2, x3, y3, x4, y4 = Shapes[0]:getBoundingBox()
local boxwidth = x3 - x2 
local boxheight = y2 - y1 
love.graphics.setColor(72, 160, 14) 
love.graphics.rectangle("fill", Bodies[0]:getX() - boxwidth/2, Bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
love.graphics.setColor(123, 77, 64)
love.graphics.circle("fill", Bodies[1]:getX(), Bodies[1]:getY(), Shapes[1]:getRadius(), 20)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("fill", Bodies[1]:getX(), Bodies[1]:getY() + boxheight/4, Shapes[2]:getRadius(), 20)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("fill", Bodies[1]:getX() + boxwidth/25, Bodies[1]:getY() + boxheight/4, Shapes[3]:getRadius(), 20)
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", Bodies[0]:getX() - boxwidth/44, Bodies[0]:getY() - boxheight/8, boxwidth, boxheight)
end
Sorry about the length.
Both rectangles are in the same place.

Code: Select all

	Shapes[0] = love.physics.newRectangleShape(Bodies[0], 0, 0, 750, 50, 0)
	...
	Shapes[4] = love.physics.newRectangleShape(Bodies[0], 0, 0, 750, 50, 0)
I really recommend tying your drawing code to the actual location of shapes/bodies, rather than hardcoding them where you expect them to be.

Also, requiring fullscreen = bad. I almost didn't look into your problem because of that.
Kurosuke needs beta testers
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Rectangles

Post by BlackBulletIV »

tentus wrote:I really recommend tying your drawing code to the actual location of shapes/bodies, rather than hardcoding them where you expect them to be.
With physical rectangles, one of the best ways to draw shapes exactly how they are is by using PolygonShape:getPoints:

Code: Select all

love.graphics.polygon("fill", shape:getPoints())
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests