Getting Started with dmc-trajectory

Get up-and-running quickly with dmc-trajectory.

This Quick Guide gives a brief overview of how to use dmc-trajectory to move objects in a ballistic trajectory (parabolic motion). There are also support methods to calculate different aspects of trajectory and motion.

More-Examples
Besides the examples shown in this document, there are other, more complete examples which can be run on the Corona simulator or a mobile device. These are located in the folder examples/dmc_trajectory/ which comes bundled with DMC Corona Library.

Table of Contents

{toc:style=disc|indent=20px|minLevel=2|exclude=Table of Contents|printable=false}

dmc-trajectory Basics

The main intent of dmc-trajectory is to provide an easy way to have objects follow parabolic trajectories. The library has a single, simple method to do this, but it also has other methods which can be used to obtain raw trajectory calculations if desired.

Setup

The library should be imported like any other via the require() statement.

{code:language=none|title=Import dmc_dragdrop}local Trajectory = require( "dmc_library.dmc_trajectory" )```

When importing dmc-trajectory, the module returns the Trajectory object which holds several static methods. Because the methods are static, that means all method calls should be accessed with the Trajectory object like so:

{code:language=none|title=Accessing Static Methods}local Trajectory = require( "dmc_library.dmc_trajectory" )

Trajectory.methodCall( ... )


## Basic Motion Using move()

The main functionalty of this library is contained in the static method `move()`. The method signature is the following:

{code:language=none|title=move() Overview}local Trajectory = require( "dmc_library.dmc_trajectory" )

Trajectory.move( object,{ params_here... })

As you can see, it requires two parameters in the method call: the object to be moved and a table of parameters for the motion.

Here is a table of the possible parameters, their type and description: || Name || Value || Description || | time | int | time in milliseconds to perform the transition | | pBegin | table | x,y coordinates of point for beginning of transition | | pEnd | table | x,y coordinates of point for end of transition | | height | int | top of trajectory in pixels, measured from the highest of the points. The value of height must be greater than zero. | | rotate | boolean | (optional) whether or not to automatically rotate object, matching the angle of the path at time t. default is false | | onComplete | function | (optional) a function reference to call at end of transition | The basics of the method can be easily shown in a picture:

text

Examples

These examples can be copy and pasted into a Corona project's main.lua file.

Simple Example

Here is a simple example using the Trajectory library which includes all of the required parameters:

{code:language=none|title=Simple Trajectory Example} local Trajectory = require( "dmc_library.dmc_trajectory" )

local begin_point, end_point ={ 50, 300 },{ 280, 100 }

local object = display.newCircle( begin_point[1], begin_point[2], 5 ) object:setFillColor( 1, 0, 0 )

Trajectory.move( object,{ time=1000, pBegin=begin_point, pEnd=end_point, height=50 })



### Complete Example

Here is an example which uses all of the parameters:


{code:language=none|title=Complete Trajectory Example}
local Trajectory = require( "dmc_library.dmc_trajectory" )

local begin_point, end_point ={ 50, 300 },{ 280, 100 }

-- let's use a rectangle so we can see the rotation
local object = display.newRect( -10, -5, 20, 10 )
object:setFillColor( 1, 1, 0 )
object.x, object.y = begin_point[1], begin_point[2]

local cleanup = function()
    object:removeSelf()
end

Trajectory.move( object,{ time=1000, pBegin=begin_point, pEnd=end_point, height=50, rotate=true, onComplete=cleanup })

References

Here are some of the useful web pages used as references.