End of a rotated rectangle

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
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

End of a rotated rectangle

Post by Nexion »

I dont know if I'm just not doing the math right or what, but I can not for the life of me place an object at the end of a rotated rectangle.
I've tried doing a few different methods of trig and whatnot to find where I need to place the new object, but it just wont work.

What I'm currently trying is the only thing that gives me a close result

Code: Select all

function update(dt) 
	world:update(dt)
	
	mPosX, mPosY = love.mouse.getPosition()
	
	lbPosX, lbPosY = LBarrelBody:getPosition()
	mbPosX, mbPosY = MBarrelBody:getPosition()
	rbPosX, rbPosY = RBarrelBody:getPosition()
	
	LBarrelBody:setAngle(math.deg(math.atan2(mPosY-lbPosY,mPosX-lbPosX)))
	MBarrelBody:setAngle(math.deg(math.atan2(mPosY-mbPosY,mPosX-mbPosX)))
	RBarrelBody:setAngle(math.deg(math.atan2(mPosY-rbPosY,mPosX-rbPosX)))

	for k, v in ipairs(bullets) do
		if v.b:getX() > 1024 or v.b:getX() < 0 or v.b:getY() < 0 then
			table.remove(bullets, k)
		end
	end
end

lastclick = 0
function mousepressed(x1, y1, button)
	if button == love.mouse_left or button == love.mouse_wheeldown or button == love.mouse_wheelup then --and lastclick + 0.1 <= love.timer.getTime() then
		lastclick = love.timer.getTime()
		
		--local spawner
		if x1 < 384 then spawner = LBarrelBody elseif x1 < 640 then spawner = MBarrelBody elseif x1 > 768 then spawner = RBarrelBody end
		if x1 < 384 then spawnerS = LBarrelShape elseif x1 < 640 then spawnerS = MBarrelShape elseif x1 > 768 then spawnerS = RBarrelShape end
		local x2, y2 = spawner:getPosition()
		local sPosX, sPosY = 512, 768
		local sAngle = spawner:getAngle()

		sPosX = x2 + math.cos(sAngle + 180) * 30 --The positions 
		sPosY = y2 + math.sin(sAngle + 180) * 30  --^                 ^

		local t = {}
		t.b = phys.newBody(world, sPosX, sPosY, 1)
		t.s = phys.newCircleShape(t.b, 5)
		t.b:setBullet(true)
		t.s:setData(t)
		table.insert(bullets, t)
		t.b:setVelocity(0,0)
	end
end
I add 180 to the angle of the barrel because it starts off at -90 due to its dimensions (LOVE is picky on its width/height crap so I just flipped them when I got the pointing at my cursor to work)
Basic trig w/ the unit square says that the position x, y based on rotation theta around the unit square is cos(x),sin(y) - and that's what im doing, only having the center be at the correct guns base then multiplying the result to scale for distance. For some reason - I get some weird non related, non scalar angle doing it this way.
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: End of a rotated rectangle

Post by Mr. Strange »

Why are you finding the positions of the three turrets each frame? Aren't they static? Try making them static if they aren't already, and see if that improves your results.

Here is my general thoughts on troubleshooting this sort of thing - you need to dramatically simplify. Try picking a point, and pointing a single fixed turret at it. Try a few points, and see if you get good results. It's possible that getting the aim point from your mouse is the problem, or perhaps you need to do mouse - turret instead of turret - mouse for your atan2 functions.

Get it down to one function call and a single variable pair, if at all possible. There is a problem there somewhere.

--Mr. Strange
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

Re: End of a rotated rectangle

Post by Nexion »

All that code does is it gets what turret to fire based on the mouse position, and attempts to get the fire position based on that.

The balls do appear around the correct cannon, just not at the end of the barrels.
I tried using just the angle from the math.atan bit in the cos to find the position, thinking it was a problem with LOVEs angle system, but that didn't help any.
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

Re: End of a rotated rectangle

Post by Nexion »

I just switched around my orientation in my atan functions to turret - mouse and I still get the same thing, so what wasn't it.

Reguardless of where I'm clicking or what turret is firing, it's still screwing up
u9_
Citizen
Posts: 54
Joined: Thu Oct 23, 2008 7:12 am

Re: End of a rotated rectangle

Post by u9_ »

This sounds like a typical degrees<-->radians problem. I have noticed that many of LOVE works with degrees, yet Lua, and most other languages work with radians. Make sure you give radians as input to sine and cosine.

/u9

(I haven't tried... Just guessing from experience)
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

Re: End of a rotated rectangle

Post by Nexion »

OH MY SWEET BABY JESUS THANK YOU!!!!!!!!!!!
I've been trying for the past 2 weeks to get this friggin thing working!

I didnt think that the cos and sin functions used radians - because I thought I remembered from my experiences in GMod Lua them being in degrees.
u9_
Citizen
Posts: 54
Joined: Thu Oct 23, 2008 7:12 am

Re: End of a rotated rectangle

Post by u9_ »

You're welcome. It's a typical problem actually. I have had this problem so many times myself, I guess I got used to the symptoms.
I didnt think that the cos and sin functions used radians
Well you know what they say: Assumption is the mother of all f***ups ;)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 26 guests