Hey,I have a ship in space that I am controlling using a only an up arrow for forward and left and right for rotate, like asteroids. What I need is how to make the ship always go in the direction the nose is pointed using the up arrow, and not go up.
Thanks [import]uid: 31005 topic_id: 7168 reply_id: 307168[/import]
Remember learning about right triangles in highschool? Y’know, stuff like tangent = opposite / adjacent. Well the reverse of that is atan; Corona comes with a bunch of math functions you can lookup in the API, including that. Get atan() of the ship’s angle to get the X and Y components of the force to apply. [import]uid: 12108 topic_id: 7168 reply_id: 25208[/import]
Thanks for the direction. I can see I have a lot of studying to do. It’s been a long time since I worked on that stuff. [import]uid: 31005 topic_id: 7168 reply_id: 25232[/import]
Here’s the physics formula I used for my game, variables changed to protect the innocent:
[lua]local scaleAmount = 10
local degreeOffset = 180
ship:applyForce( scaleAmount * math.sin( math.rad( degreeOffset + ship.rotation ) ), -scaleAmount * math.cos( math.rad( degreeOffset + ship.rotation ) ), ship.x, ship.y )[/lua]
If it’s totally moving in the wrong direction of the current angle, change the degreeOffset. If it’s moving too fast, drop down the scaleAmount. [import]uid: 6084 topic_id: 7168 reply_id: 25435[/import]
Thanks for the code. I have tried a number of ways to enter it in but can’t seem to make it work. Where exactly should I put it.
Thanks
Here’s the applicable part of the code I am using(most of it is borrowed).
[blockcode]
display.setStatusBar(display.HiddenStatusBar)
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
physics.setDrawMode(“normal”)
local gameUI = require(“gameUI”)
– set ship speed
local rotationz = 0
local motionx = 0
local motiony = 0
local speed = 3
local thrust = .1
local scaleAmount = 10
local degreeOffset = 180
–add arrow buttons
local up = display.newImage (“upx.png”)
up.x = 70
up.y = 400
local down = display.newImage (“downx.png”)
down.x = 70
down.y = 450
local left = display.newImage (“leftx.png”)
left.x = 30
left.y = 425
local right = display.newImage (“rightx.png”)
right.x = 110
right.y = 425
–enter ship
local ship = display.newImage( “ship.png” )
ship:scale(.2,.2)
ship:setReferencePoint(display.CenterReferencePoint)
ship.x = 160
ship.y = 240
physics.addBody( ship, “normal”, {density=3, friction=3,radius=35})
– Make the objects instance respond to touch events
thrust = ship.rotation
–move ship
local function moveship (event)
ship.rotation = ship.rotation + rotationz
ship.y = ship.y + motiony
end
Runtime:addEventListener(“enterFrame”, moveship)
function up:touch()
–rotationz = 0
motiony = -speed
end
up:addEventListener(“touch”, up)
function down:touch()
rotationz = 0
motiony = speed
end
down:addEventListener(“touch”, down)
function left:touch()
rotationz = -speed
motiony = 0
local r = ship.rotation
r = r + 0
if(r <= 0) then
r = r + 360
end
ship.rotation = r
print(ship.rotation)
end
left:addEventListener(“touch”,left)
function right:touch()
rotationz = speed
motiony = 0
local r = ship.rotation
r = r + 0
if(r >= 360) then
r = r - 360
end
ship.rotation = r
print(ship.rotation)
end
right:addEventListener(“touch”,right)
[blockcode] [import]uid: 31005 topic_id: 7168 reply_id: 25531[/import]
Thanks a ton BeyondtheTech,
I finally figured out where to put it and it works great. Also worked perfectly for firing my weapon. [import]uid: 31005 topic_id: 7168 reply_id: 25728[/import]