Trajectory Problem

I am having trouble showing the trajectory for an object. I put my code below, does anyone see what i did wrong?

[code]


– main.lua


_W, _H = display.contentWidth, display.contentHeight

display.setStatusBar( display.HiddenStatusBar )

local physics = require “physics”
physics.start()
physics.setScale(30)
physics.setGravity(0, 9.8) – Standard Earth gravity
physics.setDrawMode( “hybrid” )

local shape = display.newRect(0, 0, 30, 30)
shape.strokeWidth = 0
shape:setFillColor(140, 140, 140)

shape.x = 0
shape.y = 40

physics.addBody(shape, “kinematic”, { bounce = 0, friction = 0, density = 1 })

local function drawTrajectory( )
local area = shape.contentWidth / 30 * shape.contentHeight / 30
local mass = area * 1
local velocity = 200

for time=0, 10, 0.1 do
local myCircle = display.newCircle(0, 0, 4)
myCircle:setFillColor(140, 140, 140)
myCircle.x = 0 + velocity * time

myCircle.y = 40 + (9.8 * time * time / 2)
end
end

local function throw( event )
if event.phase == “ended” then
print(“THROWING”)
drawTrajectory()
shape.bodyType = “dynamic”
shape:setLinearVelocity(200, 0)
end
end

Runtime:addEventListener( “touch”, throw )
[/code] [import]uid: 26491 topic_id: 31484 reply_id: 331484[/import]

Hey, this is how I might tackle the problem (although I’m a tad bit new at this still)…

  
\_W, \_H = display.contentWidth, display.contentHeight  
   
display.setStatusBar( display.HiddenStatusBar )  
   
local physics = require "physics"  
physics.start()  
physics.setScale(30)  
physics.setGravity(0, 9.8) -- Standard Earth gravity  
physics.setDrawMode( "hybrid" )  
   
local shape = display.newRect(0, 0, 30, 30)  
shape.strokeWidth = 0  
shape:setFillColor(140, 140, 140)  
   
shape.x = 0  
shape.y = 40  
   
physics.addBody(shape, "kinematic", { bounce = 0, friction = 0, density = 1 })  
   
local function drawTrajectory( )  
  
 local myCircle = display.newCircle(0, 0, 4)  
 myCircle:setFillColor(140, 140, 140)  
 myCircle.x = shape.x  
 myCircle.y = shape.y  
end  
  
local function throw( event )  
 if event.phase == "ended" then  
 print("THROWING")  
 drawTrajectory()  
 shape.bodyType = "dynamic"  
 shape:setLinearVelocity(200, 0)  
 local drawTimer = timer.performWithDelay(75, drawTrajectory, 20)  
 end  
end  
   
Runtime:addEventListener( "touch", throw )  
  

I’m basically just taking your circle and placing it where your object is at a set interval (in this case 75ms). I picked the 20 intervals arbitrarily so you may have trouble adapting this code for different trajectory lengths, but hey, this may give you a starting place.

Hope this is somewhat helpful,

-Brandon [import]uid: 136211 topic_id: 31484 reply_id: 125788[/import]

That does work well, the issue is i am trying to show the person playing the game where their object will land before they release it. That’s why i was trying to use the equations to figure it out. Any idea how i might be able to handle that? [import]uid: 26491 topic_id: 31484 reply_id: 125789[/import]

Ah, I see. You want to show a prediction of the trajectory, not the trajectory as it’s happened. Now I see why you were involving the physics calculations.

That one’s a bit over my head at the moment. Hopefully someone else has an idea of how to tackle that.

Good luck! [import]uid: 136211 topic_id: 31484 reply_id: 125791[/import]

Hi there,

I haven’t tried this myself, but I suspect that you will also need to account for the physics scale (which you have set to 30) in your calculations. The physics engine is based on metric units, while the display objects are based in pixels, and the physics scale is the relation between. In other words, a velocity of 200 is not 200 pixels per second (which is how your function interprets it), it’s 200 meters per second. As a guess, in your drawTrajectory function, try dividing the velocity and the 9.8 gravity acceleration by the scale.

  • Andrew [import]uid: 109711 topic_id: 31484 reply_id: 125796[/import]

Andrew & Brandon,
Thanks for all your help guys. Brandon, what you gave me wasn’t exactly what i needed, but it gave me a great way to verify my predictive trajectory. Andrew, you gave me the last piece of the puzzle. Here is the working version for anyone that would like to know how to do this. The blue dots are the predictive trajectory and the grey dots are where the object actually travelled. Again thanks for all your help!

[code]
_W, _H = display.contentWidth, display.contentHeight

display.setStatusBar( display.HiddenStatusBar )

local physics = require “physics”
physics.start()
physics.setScale(30)
physics.setGravity(0, 9.8) – Standard Earth gravity
physics.setDrawMode( “hybrid” )

local shape = display.newRect(0, 0, 80, 80)
shape.strokeWidth = 0
shape:setFillColor(140, 140, 140)

shape.x = 0
shape.y = 40

physics.addBody(shape, “kinematic”, { bounce = 0, friction = 0, density = 1 })

local function drawTrajectory( )
local myCircle = display.newCircle(0, 0, 4)
myCircle:setFillColor(140, 140, 140)

print( "X: " … shape.x … " Y: " … shape.y )

myCircle.x = shape.x
myCircle.y = shape.y
end

local function drawPredictiveTrajectory( )
local velocity = 300

for time=0, 40, 2 do
local myCircle = display.newCircle(0, 0, 4)
myCircle:setFillColor(50, 50, 140)
myCircle.x = 0 + velocity / 30 * time

myCircle.y = 40 + (9.8 / 30 * time * time / 2)
end
end

local function throw( event )
if event.phase == “ended” then
print(“THROWING”)
drawPredictiveTrajectory()
drawTrajectory()
shape.bodyType = “dynamic”
shape:setLinearVelocity(300, 0)
local drawTimer = timer.performWithDelay(75, drawTrajectory, 20)
end
end

Runtime:addEventListener( “touch”, throw )
[/code] [import]uid: 26491 topic_id: 31484 reply_id: 125854[/import]

Awesome! Good to see you got it working. Glad I was able to help, at least a little bit. [import]uid: 136211 topic_id: 31484 reply_id: 125859[/import]

Hey, this is how I might tackle the problem (although I’m a tad bit new at this still)…

  
\_W, \_H = display.contentWidth, display.contentHeight  
   
display.setStatusBar( display.HiddenStatusBar )  
   
local physics = require "physics"  
physics.start()  
physics.setScale(30)  
physics.setGravity(0, 9.8) -- Standard Earth gravity  
physics.setDrawMode( "hybrid" )  
   
local shape = display.newRect(0, 0, 30, 30)  
shape.strokeWidth = 0  
shape:setFillColor(140, 140, 140)  
   
shape.x = 0  
shape.y = 40  
   
physics.addBody(shape, "kinematic", { bounce = 0, friction = 0, density = 1 })  
   
local function drawTrajectory( )  
  
 local myCircle = display.newCircle(0, 0, 4)  
 myCircle:setFillColor(140, 140, 140)  
 myCircle.x = shape.x  
 myCircle.y = shape.y  
end  
  
local function throw( event )  
 if event.phase == "ended" then  
 print("THROWING")  
 drawTrajectory()  
 shape.bodyType = "dynamic"  
 shape:setLinearVelocity(200, 0)  
 local drawTimer = timer.performWithDelay(75, drawTrajectory, 20)  
 end  
end  
   
Runtime:addEventListener( "touch", throw )  
  

I’m basically just taking your circle and placing it where your object is at a set interval (in this case 75ms). I picked the 20 intervals arbitrarily so you may have trouble adapting this code for different trajectory lengths, but hey, this may give you a starting place.

Hope this is somewhat helpful,

-Brandon [import]uid: 136211 topic_id: 31484 reply_id: 125788[/import]

That does work well, the issue is i am trying to show the person playing the game where their object will land before they release it. That’s why i was trying to use the equations to figure it out. Any idea how i might be able to handle that? [import]uid: 26491 topic_id: 31484 reply_id: 125789[/import]

Ah, I see. You want to show a prediction of the trajectory, not the trajectory as it’s happened. Now I see why you were involving the physics calculations.

That one’s a bit over my head at the moment. Hopefully someone else has an idea of how to tackle that.

Good luck! [import]uid: 136211 topic_id: 31484 reply_id: 125791[/import]

Hi there,

I haven’t tried this myself, but I suspect that you will also need to account for the physics scale (which you have set to 30) in your calculations. The physics engine is based on metric units, while the display objects are based in pixels, and the physics scale is the relation between. In other words, a velocity of 200 is not 200 pixels per second (which is how your function interprets it), it’s 200 meters per second. As a guess, in your drawTrajectory function, try dividing the velocity and the 9.8 gravity acceleration by the scale.

  • Andrew [import]uid: 109711 topic_id: 31484 reply_id: 125796[/import]

Andrew & Brandon,
Thanks for all your help guys. Brandon, what you gave me wasn’t exactly what i needed, but it gave me a great way to verify my predictive trajectory. Andrew, you gave me the last piece of the puzzle. Here is the working version for anyone that would like to know how to do this. The blue dots are the predictive trajectory and the grey dots are where the object actually travelled. Again thanks for all your help!

[code]
_W, _H = display.contentWidth, display.contentHeight

display.setStatusBar( display.HiddenStatusBar )

local physics = require “physics”
physics.start()
physics.setScale(30)
physics.setGravity(0, 9.8) – Standard Earth gravity
physics.setDrawMode( “hybrid” )

local shape = display.newRect(0, 0, 80, 80)
shape.strokeWidth = 0
shape:setFillColor(140, 140, 140)

shape.x = 0
shape.y = 40

physics.addBody(shape, “kinematic”, { bounce = 0, friction = 0, density = 1 })

local function drawTrajectory( )
local myCircle = display.newCircle(0, 0, 4)
myCircle:setFillColor(140, 140, 140)

print( "X: " … shape.x … " Y: " … shape.y )

myCircle.x = shape.x
myCircle.y = shape.y
end

local function drawPredictiveTrajectory( )
local velocity = 300

for time=0, 40, 2 do
local myCircle = display.newCircle(0, 0, 4)
myCircle:setFillColor(50, 50, 140)
myCircle.x = 0 + velocity / 30 * time

myCircle.y = 40 + (9.8 / 30 * time * time / 2)
end
end

local function throw( event )
if event.phase == “ended” then
print(“THROWING”)
drawPredictiveTrajectory()
drawTrajectory()
shape.bodyType = “dynamic”
shape:setLinearVelocity(300, 0)
local drawTimer = timer.performWithDelay(75, drawTrajectory, 20)
end
end

Runtime:addEventListener( “touch”, throw )
[/code] [import]uid: 26491 topic_id: 31484 reply_id: 125854[/import]

Awesome! Good to see you got it working. Glad I was able to help, at least a little bit. [import]uid: 136211 topic_id: 31484 reply_id: 125859[/import]