apply LinearVelocity based on objects current rotation.

Hey everyone.

I’m coding a physics based highscore game and need to apply;

myObj:setLinearVelocity(x,y)

to my player object, based on his current angle of rotation. I’ve researched the Maths and tried putting it together, but it doesn’t perform as I’d like.

The code is drag and drop except for the arrow.png, which is simply an arrow pointing to the right on the screen.

here is my code. I’m stuck around line 139.

  
physics = require("physics")  
physics.start()  
physics.setGravity(0,0)  
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.  
  
\_H = display.contentHeight;  
\_W = display.contentWidth;  
function player()  
  
player1 = display.newImage ("arrow.png")  
player1.x = \_W / 2  
player1.y = \_H / 2  
  
physics.addBody(player1, "dynamic", {density = 0, friction = 0, bounce = 0})  
player1.myName = "player1"  
  
 function setplayerRot()  
 playerRot = 0  
 local playerRotText = display.newText( "playerRot: " .. playerRot, 10, 0, nil, 15)  
 playerRotText.x = \_W / 4  
 playerRotText.y = \_H / \_H + 50  
 playerRotText:setTextColor(255,255,255)  
 function getplayerRot()  
 playerRot = player1.rotation  
 playerRotText.text = "playerRot: " .. playerRot   
 end  
 Timer1 = timer.performWithDelay(10,getplayerRot, 0)  
 end  
  
end  
  
function radians()  
 p1Radians = player1.rotation \* math.pi / 180  
 --print (p1Radians)  
end  
  
function velocity()  
 velx, vely = player1:getLinearVelocity()  
  
 vx = math.cos (p1Radians) \* velx  
 print (vx)  
  
  
end  
function buttons()  
  
function lArrow()  
  
leftArrow = display.newRect(0,0,50,50)  
leftArrow.x = \_W / \_W + 50  
leftArrow.y = \_H - 50  
leftArrow:setFillColor(0,255,0)  
leftArrow.alpha = .75  
  
ltouched = false  
  
local function ltouch(event)  
 if event.phase == "began" then  
 ltouched = true  
 elseif event.phase == "ended" then  
 ltouched = false  
end  
end  
  
local function lrot(event)  
 if ltouched == true then  
 player1.rotation = player1.rotation - 10  
 elseif ltouched == false then return false  
 end  
  
end  
  
leftArrow:addEventListener ("touch", ltouch)  
Runtime:addEventListener("enterFrame", lrot)   
  
end  
  
 function rArrow()  
  
local rightArrow = display.newRect(0,0,50,50)  
rightArrow.x = \_W - 50  
rightArrow.y = \_H - 50  
rightArrow:setFillColor(0,255,0)  
rightArrow.alpha = .75  
  
rtouched = false  
  
local function rtouch(event)  
 if event.phase == "began" then  
 rtouched = true  
 elseif event.phase == "ended" then  
 rtouched = false  
end  
end  
  
local function rrot(event)  
 if rtouched == true then  
 player1.rotation = player1.rotation + 10  
 elseif rtouched == false then return false  
 end   
 end  
  
rightArrow:addEventListener ("touch", rtouch)  
Runtime:addEventListener("enterFrame", rrot)   
  
end  
  
 function thrustButton()  
  
 local thrustb = display.newRect(0,0,100,50)  
 thrustb.x = \_W/2  
 thrustb.y = \_H - 50  
 thrustb:setFillColor(0, 0, 255)  
 thrustb.aplha = .75  
  
 ttouched = false  
  
local function ttouch(event)  
 if event.phase == "began" then  
 ttouched = true  
 elseif event.phase == "ended" then  
 ttouched = false  
 cando = true  
end  
end  
  
local function ttap(event)  
 if ttouched == true then  
 if cando == true then  
  
 vel = 10 -- this is the arbitrary amount of velocity to increase player motion by.  
 vely = -500  
 p1Rads = player1.rotation \* math.pi / 180 -- step 1  
  
 velX = math.cos (p1Rads) \* vel -- upper case (velX/velx) to feed into obj:setLinearVelocity(X,Y)  
 velY = math.sin (-p1Rads) \* vel  
 print(velX,velY,player1.rotation)  
  
  
  
 player1:setLinearVelocity(velX,velY)  
  
 local function stopMovement()  
 player1:setLinearVelocity(0,0)  
 end  
  
 Timer1 = timer.performWithDelay(2000,stopMovement, 1)  
 cando =false  
 end  
 elseif ttouched == false then return false  
 end   
end  
  
thrustb:addEventListener ("touch", ttouch)  
Runtime:addEventListener("enterFrame", ttap)   
  
end  
lArrow()  
rArrow()  
thrustButton()  
end  
function startGame()  
  
player()  
buttons()  
setplayerRot()  
  
end  
  
startGame()  

Thanks in advance. [import]uid: 67933 topic_id: 16610 reply_id: 316610[/import]

If the issue is what I think it is (up and down movement being confused) try on line 144 changing “-p1Rads” to “p1Rads”.

That’s just a quick glance, others may be able to help more. (Especially if you spell out the specific problem.)

Peach :slight_smile: [import]uid: 52491 topic_id: 16610 reply_id: 62517[/import]