Shape is not rotating

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
gabriel_kstro
Prole
Posts: 2
Joined: Mon Jan 09, 2017 4:37 am

Shape is not rotating

Post by gabriel_kstro »

My shape is not rotating when I use body:setAngle(). The image on the screen rotates and the value of the variable also but the shape appears to continue in the same place. The shape and physics not rotating with image. Can someone help me?

Code: Select all

function love.load()
 	retangulo = {
 		x=400,
 		y=50,
 		altura =100,
 		largura=200,
 		img = love.graphics.newImage("carro.png")
 	}
 	retangulo2 = {
 		x=50,
 		y=50,
 		altura =100,
 		largura=200,
 		img = love.graphics.newImage("carro2.png")
 	}

 	
 	mundo = love.physics.newWorld(0,0,false)
 	
 	retangulo.body = love.physics.newBody( mundo, retangulo.x, retangulo.y, "dynamic" )
 	retangulo.shape = love.physics.newRectangleShape(0,0, retangulo.img:getWidth(),retangulo.img:getHeight() )
 	retangulo.fixture = love.physics.newFixture( retangulo.body, retangulo.shape,1)


 	retangulo2.body = love.physics.newBody( mundo, retangulo2.x, retangulo2.y, "dynamic" )
 	retangulo2.shape = love.physics.newRectangleShape( retangulo2.img:getWidth(),retangulo2.img:getHeight())
 	retangulo2.fixture = love.physics.newFixture( retangulo2.body, retangulo2.shape)
 	mundo:setCallbacks(beginContact, endContact, preSolve, postSolve)

 	retangulo.body:setAngle(-amath.rad(90))

end

function love.update(dt)
	mundo:update(dt)

	if love.keyboard.isDown ("up") then
			retangulo.body:setY(retangulo.body:getY()-5)
	elseif love.keyboard.isDown ("down") then
			retangulo.body:setY(retangulo.body:getY()+5)
	end
	if love.keyboard.isDown ("right") then
			retangulo.body:setX(retangulo.body:getX()+5)
	elseif love.keyboard.isDown ("left") then
			retangulo.body:setX(retangulo.body:getX()-5)
	end

	print(math.deg(retangulo.body:getAngle()))
end

function love.draw()
	love.graphics.draw(retangulo.img,retangulo.body:getX(),retangulo.body:getY(),retangulo.body:getAngle(),1,1)
	love.graphics.draw(retangulo2.img,retangulo2.body:getX(),retangulo2.body:getY(),retangulo2.body:getAngle())
end

function beginContact(a, b, coll)
 	print("crash")
end
 
function endContact(a, b, coll)
 
end
 
function preSolve(a, b, coll)
 
end
 
function postSolve(a, b, coll, normalimpulse, tangentimpulse)
 
end
nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

Re: Shape is not rotating

Post by nyenye »

Hii, first of all try writing your code in english, its easier for people of the forum to help you.

About the question, have you tried to apply torque to the body? (https://love2d.org/wiki/Body:applyTorque) Does it rotate?

Havent used love2d's implementation of box2d so can't really say much.
User avatar
ken.athomos
Citizen
Posts: 77
Joined: Fri Aug 05, 2016 10:13 am
Location: Philippines

Re: Shape is not rotating

Post by ken.athomos »

Rough Translation just in case :)

rectangulo = rectangle
altura = height (or altitude)
largura = width (or length)
mundo = world

If you're wondering how I translated this, I'm Filipino. Most of our modern day words are Spanish.
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: Shape is not rotating

Post by 0x72 »

I believe that it's rotating properly but you're drawing it wrong.

retangulo.body:getX(),retangulo.body:getY() seams to give you the center of the shape (by default at least) while love.graphics.draw's x and y arguments are supposed to be top-left corner.

You can offset it with ox and oy of love.graphics.draw.

Code: Select all

love.graphics.draw(retangulo.img,retangulo.body:getX(), retangulo.body:getY(), retangulo.body:getAngle(), 1, 1, retangulo.largura/2, retangulo.altura/2)
I've never used box2d with love so I might be wrong. In case it helps you: I've used this nice thing: viewtopic.php?t=77140 :) (I had to pcall it for some reason though)

edit:

Code: Select all

function drawRect(r)
  love.graphics.draw(r.img, r.body:getX, r.body:getY, r.body:getAngle(), 1, 1, r.largura/2, r.altura/2)
end
nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

Re: Shape is not rotating

Post by nyenye »

Have you tried to draw as this tutorial does? https://love2d.org/wiki/Tutorial:Physics

I can't say it's eficient but at least it should be accurate to the body state/angle.

Good luck!
gabriel_kstro
Prole
Posts: 2
Joined: Mon Jan 09, 2017 4:37 am

Re: Shape is not rotating

Post by gabriel_kstro »

It worked! The shape was in the wrong place and the drawing was adjusted. The debug tool is great! Thanks
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 208 guests