-
Notifications
You must be signed in to change notification settings - Fork 48
Tutorial: Physics Editor in RapaNui
#RapaNui tutorial:
##Setting up Physics Editor using the Rapanui framework
As Prerequisite you should already know how to correctly run Rapanui's framework examples.Check the README under your RapaNui copy.
Let's create a simple demo of falling physics objects. You can download all the demo files here: http://ymobe.github.com/rapanui/tutorial_physics_editor/demo.zip
##1:Try the demo
To run the demo simply replace the RapaNui's default main.lua and config.lua with the ones you find in demo.zip.
Also add to your Rapanui main directory the folder resources you can find in the demo.zip.
If you run the demo it should look like this:
##2: Let's write the code step by step
###What to do:
In your favourite lua editor open main.lua and the config.lua which is in the same directory.
What's inside this two files: config.lua contains the setup for the screensize and main.lua contains the code of the demo.
###Setting screen size:
This demo runs in a 580x410 window. The config.lua in the example directory, the same directory as main.lua, will handle this with:
module(..., package.seeall) sizes = {} sizes["PE_Demo"]={580,410,580,410} landscape = false device = "PE_Demo"
As you can see from the code above, we set a size for the screen to be viewed and the landscape mode as false. A window 580x410 will be opened.
The table inside table named "PE_Demo" contains the screen size.
That's it, we have done with config.lua
###Importing the correct files:
Now let's write the real demo, on main.lua file. Let's see imports:
require("RNFactory") require("RNListeners") require("RNPhysics") require("RNMainThread") require("RNThread") The above lines load the correct files from Rapanui framework.
RNlisteners, RNMainThread and RNThread are used to call actions which make objects "fall".
RNPhysics will handle the physics world and simulation.
RNFactory will help in creating Rapanui objects.
###Starting the physics simulation
Physics simulation is simply started with:
RNPhysics.start()
Now the Physics world is created and running.
###Creating a background image
Thanks to RNFactory we can easily add images with:
RNFactory.createImage("resources/bkg_cor.png");
###Loading Physics Editor datas:
To create Physics Editor's objects we need to first load PE's data from the published file for Rapanui (check resources dir):
local physicsData = (require"resources/PhysicsData").physicsData()
We store this data in the local variable "physicsData", to use it later.
###Creating the floor
Now we have all PE's data stored in "physicsData" we can create all the objects we want with the information from PhysicsData.lua
To create a floor we need to:
- Create the image
- Move and optionally name it
- Make the floor a physics object. 3 lines are enough for this:
local floorImage = RNFactory.createImage("resources/floor.png"); floorImage.x = 290; floorImage.y = 375; floorImage.name = "floor" RNPhysics.createBodyFromImage(floorImage, "static", physicsData:get("floor")) line 1 creates the floor. line 2 moves and renames it (else its name would have been "floor.png"). line 3 creates a body from the given image and physics data with the optional type attribute "static" (needed for a static floor).
"physicsData:get("floor")" means we are loading all the information under the name "floor" from physicsData.
At this point the demo, if everything is fine, will look like:
And the code will look like:
require("RNFactory") require("RNListeners") require("RNPhysics") require("RNMainThread") require("RNThread")
RNPhysics.start()
RNFactory.createImage("resources/bkg_cor.png");
local physicsData = (require"resources/PhysicsData").physicsData()
local floorImage = RNFactory.createImage("resources/floor.png"); floorImage.x = 290; floorImage.y = 375; floorImage.name = "floor" RNPhysics.createBodyFromImage(floorImage, "static", physicsData:get("floor"))
###Creating falling objects:
To create falling objects we need a function to be called repeatedly which will spawn random objects from a table of elements.
It won't be so hard because images names are the same as the ones in physicsData, and I suggest you to do it always this way. So we can create our table:
local foodTable = { 'drink', 'hamburger', 'hotdog', 'icecream', 'icecream2', 'icecream3' } And now we can write a function to take a random item from this table and create a physics object:
function createFallingObjects() local item = foodTable[math.random(#foodTable)] local object = RNFactory.createImage("resources/" .. item .. ".png"); object.x = math.random(580); object.name = item RNPhysics.createBodyFromImage(object, physicsData:get(item)) end Objects are created as the same way as the floor: we create an image, if we want we move it and rename it, so we make the image a physics object giving the factory the image and the correct data from PhysicsEditor storing variable(this time we don't need to specify "static" or "dynamic", by default objects are created with the "dynamic" attribute)
Now if we call the above function we'll see a random object falling down:
createFallingObjects()
But our function has to be called repeatedly, so we need to call a Rapanui's timed action to handle this:
RNMainThread.addTimedAction(2000, createFallingObjects, -1)
As you can imagine the above line loop repeats (because of "-1") createFallingObjects() each 2000 time steps.
###The final result:
Thanks to Rapanui, Moai and Physics Editor we can create a complex physics world with just 20 lines of code!
Here the code without comments:
require("RNFactory") require("RNListeners") require("RNPhysics") require("RNMainThread") require("RNThread")
RNPhysics.start()
RNFactory.createImage("resources/bkg_cor.png");
local physicsData = (require"resources/PhysicsData").physicsData()
local floorImage = RNFactory.createImage("resources/floor.png"); floorImage.x = 290; floorImage.y = 375; floorImage.name = "floor" RNPhysics.createBodyFromImage(floorImage, "static", physicsData:get("floor"))
local foodTable = { 'drink', 'hamburger', 'hotdog', 'icecream', 'icecream2', 'icecream3' }
function createFallingObjects() local item = foodTable[math.random(#foodTable)] local object = RNFactory.createImage("resources/" .. item .. ".png"); object.x = math.random(580); object.name = item RNPhysics.createBodyFromImage(object, physicsData:get(item)) end
createFallingObjects()
RNMainThread.addTimedAction(2000, createFallingObjects, -1) And if we run the demo we should see something like:
Now you can try some changes to code and shapedefs.pes and see what happens!
Thank you for using Physics Editor and Rapanui.
tutorial by Rapanui Team