Hello everyone, I’m very new here and would really appreciate any help I can get.
Been googling and trying to solve this for hours now without any luck.
I have a small “game” consisting of two things. One small rectangle which moves at a constant speed towards it direction at all times. When the user holds down the mouse the rectangle turns by raising the object.rotation by one every tick. The other object is a small circle in the center of the screen.
My problem is that I want the rectangle to rotate around the circle, and the closer the distance to the circle is, the better it will be able to make the turn. So a rectangle that’s far away from the circle will have to make a circle-like turn with a way bigger radius than one that’s close to the circle (which will be able to make very quick turns). I’ve been trying alot but I dont have any good idea on how to solve this. I hope my explanation here is enough lol, a bit messy.
I attached a picture where I try to show what I mean. The red line is the critical angle for the rectangle object. At any time it must not get below this angle so that it misses center of the circle, the circle origo is always its “goal” or whatever to call it.
Thanks in advance!
Under here comes code:
[lua]local background = display.newRect( 0, 0, 480, 320 )
background:setFillColor( 0, 0, 0 )
–The Dot
local dot
–The playerangle
local player
–In case you need physics
local physics = require “physics”
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( “hybrid” )
–Math functions
local Cos = math.cos
local Sin = math.sin
local Rad = math.rad
local Atan2 = math.atan2
local Deg = math.deg
local radius = 10
–local angle = 90 --Start angle
–local function getYref()
– local xDist = math.abs(dot.x - player.x)
– local yDist = math.abs(dot.y - player.y)
– local totalDist = math.sqrt( xDist*xDist + yDist*yDist )
– print (totalDist)
– return totalDist
–end
– Add a method to the foo object to handle the touch event
local function onTouch( event )
if event.phase == “began” then
display.getCurrentStage():setFocus( player )
player.isFocus = true
Runtime:addEventListener( “enterFrame”, doTurn )
elseif player.isFocus then
if event.phase == “moved” then
elseif event.phase == “ended” then
Runtime:removeEventListener( “enterFrame”, doTurn )
display.getCurrentStage():setFocus( nil )
player.isFocus = false
end
end
end
function doTurn()
--tempX = dot.x + Cos(Rad(angle)) * radius
--tempY = dot.y + Sin(Rad(angle)) * radius
player.rotation = player.rotation+1;
--angle = angle+1
end
local function moveForward()
player.x = player.x + Cos(math.rad(player.rotation))*1
player.y = player.y + Sin(math.rad(player.rotation))*1
end
dot = display.newCircle(display.contentWidth/2,display.contentHeight/2,5)
dot:setFillColor(200,0,0)
–The playerangle (could be an image)
player = display.newRect(30,20,20, 30)
player:setReferencePoint(display.CenterReferencePoint)
physics.addBody(player)
–Run the animation
Runtime:addEventListener( “enterFrame”, moveForward )
Runtime:addEventListener( “touch”, onTouch)[/lua]