Hi
This is my first time asked a questions in this way and hence I am not sure if theirs a certain way to post it but here it goes.
So I am creating a game in which the main player moves by moving your finger across the screen. Specifically, the player main body is connected to the player bottom (like a rocket ship) by a pivot joint in which as the person moves his finger around the screen, only the player bottom responds to the touch by rotating around its pivot joint on player main body. At the same time a linear impulse is applied at the angle which the player bottom is rotated. However the problem is that if I stop moving my finger across the screen while it still in contact with the screen, no linear impulse is being applied and also the player bottom also doesn’t hold its rotational value and is simply ‘released’ where is falls down due to gravity. I believe I need to constantly update is by creating a variable with no value and assigning the rotational value to it and then returning it however I have no clue how to do this. I would really appreciate the help. I am also coding it using OOP (Object Oriented Programming) however I assume that doesn’t affect this specific problem. My code is pasted below
[lua]
local Player = class()
Player.__name = “Player”
function Player:__init(xValue, yValue)
self.xValue = xValue
self.yValue = yValue
– self:drawPlayer()
end
function Player:drawPlayerBody()
local playerImage = display.newImage(“Images/playerSprite.png”)
– place the dog in the correct location on the screen
playerImage:scale(0.5,0.5)
playerImage.x = self.xValue
playerImage.y = self.yValue
print(playerImage.anchorX, playerImage.anchorY)
physics.addBody( playerImage, “dynamic”, {isSensor = false, radius = 30, friction = 50})
– local mainBody = playerImage.contentBounds
local playerBottomImage = display.newImage(“Images/playerBottomSpr.png”, playerImage.x, playerImage.y)
playerBottomImage.anchorY = 0
playerBottomImage:toBack()
physics.addBody( playerBottomImage, “dynamic”, {isSensor = false, density = 0.75, bounce = 0, friction = 50})
pivotJoint = physics.newJoint( “pivot”, playerImage, playerBottomImage, playerImage.x, playerImage.y)
pivotJoint.isLimitEnabled = true
pivotJoint:setRotationLimits( -50, 50 )
– playerImage.angularDamping = 35
local theAngle = math.rad(playerBottomImage.rotation + 90) – we need angle in radians
local xComp = math.cos(theAngle) – the x component
local yComp = -math.sin(theAngle) – the y component is negative because
– “up” the screen is negative
local adjustment = adjustment
local forcePower = 0.2
local function movePlayer(event)
display.getCurrentStage():setFocus( event.target )
if(event.phase == “began”) then
print("--------------X point " … playerBottomImage.x … "Y point " … playerBottomImage.y)
local deltaX = event.x - (playerBottomImage.x)
local deltaY = event.y - (playerBottomImage.y)
adjustment = math.deg(math.atan2(deltaY,deltaX)) - playerBottomImage.rotation
playerBottomImage:applyLinearImpulse(forcePower*xComp, forcePower*yComp, playerBottomImage.x, playerBottomImage.y)
– playerImage:applyLinearImpulse(forceMag*xComp, forceMag*yComp, playerImage.x, playerImage.y)
print ("before " … deltaX, "before "… deltaY)
print("the adjustment is " … adjustment)
print("player rotation " … playerBottomImage.rotation)
elseif(event.phase == “moved”) then
local deltaX = event.x - (playerBottomImage.x)
local deltaY = event.y - (playerBottomImage.y)
print (deltaX, deltaY)
playerBottomImage.rotation = math.deg(math.atan2(deltaY,deltaX)) - adjustment
playerBottomImage:applyLinearImpulse(forcePower*xComp, forcePower*yComp, playerBottomImage.x, playerBottomImage.y)
– playerImage:applyLinearImpulse(forceMag*xComp, forceMag*yComp, playerImage.x, playerImage.y)
elseif (event.phase == “ended”) or (event.phase == “canceled”) then
display.getCurrentStage():setFocus(event.target, nil)
event.target.isFocus = false
end
end
playerBottomImage:addEventListener(‘touch’, movePlayer)
end
[/lua]