Posts Tagged ‘Rapid Prototyping’

TexturePacker Extension for ShiVa 3D

January 14th, 2013

UPDATE: TexturePacker 3.0.6 comes with a separate Shiva 3D output format. I updated the post to reflect these changes as well as some other workflow optimizations.

My first product got published on the ShiVa Asset Store: an extension to support the awesome program TexturePacker by Andreas Löw. If you don’t know of it yet, check it out at http://www.codeandweb.com/texturepacker. Here is a screen shot of the supplied demo, showing off all the supported features:

demo

Features

  • No need to build your atlas by hand, TexturePacker does the work for you :)
  • Texturize Models
  • Texturize HUD Components

Usage

texturepackerExport

  1. Create your atlas with TexturePacker:
    • Data Format is “Shiva3D“.
    • Texture Format is TGA.
    • Allow Rotation” is only supported for texturing Models. HUDs are not supported!
    • Trim Mode is “None“.
    • Press the “Publish” Button. This will create 2 files: an XML and a PNG.
  2. Import the XML and the PNG via the Data Explorer into Shiva and add them as Resources to your game:
    importTexture  importXml

    1. Workflow Optimization: if you want to skip the importing, configure your output paths to the shiva resources folder directly! I only tested this once the resources was already created though.
  3. If texturing a Model, create a Material with the same name as the texture, configure it such that it uses the texture and also add it to the Resources of your game.

You are now ready to use the atlas!

 

Here is some example code:

local hUser = application.getCurrentUser ( )
 
application.setCurrentUserScene ( "TexturePacker" )
 
--Load atlas data:
--Use sendEventImmediate!
user.sendEventImmediate ( hUser, "TexturePacker", "onLoadAtlas", "atlasTexturePacker" )
--user.sendEventImmediate ( hUser, "TexturePacker", "onLoadAtlas", "otherAtlas" )
 
--Texturize Model via code:
user.sendEventImmediate ( hUser, "TexturePacker", "onSetObjectTexture", application.getCurrentUserSceneTaggedObject ( "obj8" ), "tp05" )
user.sendEventImmediate ( hUser, "TexturePacker", "onSetObjectTexture", application.getCurrentUserSceneTaggedObject ( "obj1" ), "tp01" )
user.sendEventImmediate ( hUser, "TexturePacker", "onSetObjectTexture", application.getCurrentUserSceneTaggedObject ( "obj2" ), "tp02" )
 
--Or use Object AI "TexturePackerAI" (done for obj3 and obj4)

 

Best you take a look at the supplied TexturePackerDemo.
Set your viewport to Android Landscape (800×480) for best results.

Here’s a snapshot of a little game I’m currently working on, utilizing the TexturePacker extension (every texture is coming out of an atlas):

overview

Feedback is welcome!

And once again, the link to the ShiVa Asset Store: http://www.shivaengine.com/store/aimodels/132-texturepacker-ai.html.

Box2D-Shiva-Wrapper: API Updates

June 4th, 2012

I was cast away from my computer nearly the whole last week, so progress wasn’t too big. I still found some time to expand the API:

isShapeSensor
isBodyStatic
isBodyDynamic
isBodyKinematic
getFirstShape
getShapeList
getShapeCategoryBits
getShapeCategoryBitsArray
getShapeMaskBits
getShapeGroupIndex
setBodyAngularVelocity

As stated in my last post, by creating an actual game with or for the Box2D plugin, it has again raised some issues and “feature requests” which I probably would have missed elsewise.

Box2D-Shiva-Wrapper: Demo Project

May 26th, 2012

Not too much news regarding the actual Box2D wrapper today. I rather spent the last days on coming up with a little demo project (proof of concept for the plugin). I already got a rough prototype up and running, but the time isn’t right yet to release any info to public. Fact is, that this demo already has brought up some issues that I fixed and besides that, I started expanding the API:

applyAngularImpulse
applyForce
applyForceToCenter
applyLinearImpulse
applyLinearImpulseToCenter
applyTorque
setBodyActive
setBodyAwake
destroyBody
getBodyLinearVelocity
getAngleBetweenPoints

Not too much, but more is coming!

Now back to some concept art (nice alternative to too much programming :) )!

Box2D-Shiva-Wrapper: Compiling Box2D As Plugin

May 16th, 2012

I will write which stones I fell over during the process of getting Box2D to compile as a ShiVa-Android-plugin. This could also be useful for anybody who wants to use STL on Android.

Disclaimer: I am far no C++, build or make file guru. If you spot anything, which could be done better, please tell me (I am willing to learn).

Enabling STL

First, modify the MyPlugin.makefile (located in “/ShiVaEditor/Data/PluginTemplate/Make/Android/”) such that CPPFLAGS looks like this:

CPPFLAGS            = -fno-rtti -I"$(ANDROID_NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/include" -I"$(ANDROID_NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include"

Then in ShiVa, regenerate your Android plugin makefile and click on recompile (RE-compile, not compile). If everything works out right, you should see the red circle next to the android plugin turned green:

Now you’re ready to export!

Export your game with the Android profile selected. This is important, because when using plugins, only the selected profile version of the plugin is packed into the STK. So if you export with the DefaultProfile, the Android plugin will probably be missing! Note: After exporting, you can always look at the yourgame.xml to see what files are packed into the STK. Just open it with your browser.

With the freshly exported STK, now create an Android project with UAT. Unzip it’s contents and make the following changes to these two files:

root/build.xml:
Add the line

APP_STL			 := gnustl_static

to where the Application.mk is defined (2 spots in the build.xml: for release and debug).

root/jni/Android.mk:
Add the line

LOCAL_LDLIBS           += -lgnustl_static

after the last LOCAL_LDLIBS assignments.

Now compile the build.xml and hope that everything works :) I have come accross some weird build errors, they are handled in the next section.

Credits to this whole procedure go to supernat on this thread in the ShiVa forum! In case you want to use STLPORT instead of GNUSTL, you can see what changes need to be made over at the forum. I stuck with GNUSTL. Also big thanks to Dominik from StarkApps.

Box2D Specific Modifications

Precompiled Headers

I don’t really have a clue, what precompiled headers do (nice rhyme eh?), but I managed to get them to compile. In the end, I had to add a #include "PrecompiledHeader.h" to nearly every Box2D source file (the compiler gave some tips/warnings that I must do so). As I had done this, the plugin started to compile for Windows. For Android, I got some weird error like “error in algorithm” or “error in stdlib.h”, which were quite misleading.

In the end, I simply MOVED the failing include statements to the precompiled header file (meaning removing them from the Box2D sources and adding them to the precompiled header file), leading to these additional include statements in the precompiled header file (~ stands for < or > …had some trouble with html editor):

#include ~stdlib.h~
#include ~cstdlib~
#include ~cstdio~
#include ~cstdarg~
#include ~climits~
#include ~cstring~
#include ~memory~
#include ~algorithm~

The funny thing now is, there’s a precompiled header file for each platform and I only added these includes to the windows one. I actually don’t know why this is working on Android, but sometimes you just have to stop asking questions :) If anybody out there has an idea, please tell me.

b2BroadPhase And std::sort

The file b2BroadPhase.h has a call to std::sort, which the Android compiler complained that he can’t find. Looking at the GNUSTL headers/templates, it read like that for GNUSTL, this isn’t in the std namespace … but I’m not sure about that one. Reading headers and templates in notepad++ isn’t a good idea with all those macros and defines. For the first version, I just commented the line out, as it’s just a performance optimization anyway.

Includes

Don’t use backslashes in include statements, use slashes! Meaning don’t write #include "Box2D\Box2D.h", instead write #include "Box2D/Box2D.h". Not sure if this would have also failed on Android, but on iOS it raised an error.

 

That was it for now. I’ll keep updating the issues list as I keep working on it and maybe I’ll have some insights on the unclear points.

Box2D-Shiva-Wrapper: The API

May 11th, 2012

As of ShiVa 1.9.x, you can’t really create data structures. So if you e.g. want to translate something by X/Y/Z, you don’t have a vector data type with the members vector.x/vector.y/vector.z meaning you then can’t call myAPI.translate(myVector). Instead you have to call myAPI.translate(X, Y, Z).

This gets a bit complicated as soon as you have tons of possible parameters, like when wrapping the Box2D b2Body class. After a bit of discussion, we came to the conclusion, that using hashtables will do the trick. The drawback is, that you have to know the name of the properties, as they are entered as a string, but warnings are logged as soon as you do something wrong … and one could create constants for the keys to make it “end user safe”. Also, the end user actually shouldn’t really need to touch this piece of code anyway as stated in my first post.

So here is how the creation of a body looks like in StoneScript:
(Please not that this code uses all the body properties – most of them are optional)

local hObject = this.getObject ( )
 
local hBodyDef = hashtable.newInstance ( )
 
if ( hBodyDef )
then
	local nPosX, nPosY, nPosZ = object.getTranslation ( hObject, object.kGlobalSpace )
	local nRotX, nRotY, nRotZ = object.getRotation( hObject, object.kGlobalSpace )
	local nAngleRad = nRotZ * 0.0174532925 --google says: 1 degree = 0.0174532925 radian :)
 
	hashtable.add ( hBodyDef, "active",             this.bActive ( ) )
	hashtable.add ( hBodyDef, "allowSleep",         this.bAllowSleep ( ) )
	hashtable.add ( hBodyDef, "angle",              nAngleRad )
	hashtable.add ( hBodyDef, "angularDamping",     this.nAngularDamping ( ) )
	hashtable.add ( hBodyDef, "angularVelocity",    this.nAngularVelocity ( ) )
	hashtable.add ( hBodyDef, "awake",              this.bAwake ( ) )
	hashtable.add ( hBodyDef, "bullet",             this.bBullet ( ) )
	hashtable.add ( hBodyDef, "fixedRotation",      this.bFixedRotation ( ) )
	hashtable.add ( hBodyDef, "gravityScale",       this.nGravityScale ( ) )
	hashtable.add ( hBodyDef, "linearDamping",      this.nLinearDamping ( ) )
	hashtable.add ( hBodyDef, "linearVelocity.x",   this.nLinearVelocityX ( ) )
	hashtable.add ( hBodyDef, "linearVelocity.y",   this.nLinearVelocityY ( ) )
	hashtable.add ( hBodyDef, "position.x",         nPosX )
	hashtable.add ( hBodyDef, "position.y",         nPosY )
	hashtable.add ( hBodyDef, "type",               this.sType ( ) )
	hashtable.add ( hBodyDef, "callbackAI",                 this.sCallbackAI ( ) )
	hashtable.add ( hBodyDef, "callbackOnBeginContact",     this.sCallbackOnBeginContact ( ) )
	hashtable.add ( hBodyDef, "callbackOnContact",          this.sCallbackOnContact ( ) )
	hashtable.add ( hBodyDef, "callbackOnEndContact",       this.sCallbackOnEndContact ( ) )
 
	maBox2d.createBody ( hObject, hBodyDef )
end

And this is how the creation of a shape looks like in StoneScript:
(Please note that the scale and center calculation isn’t required for the actual creation of the shape)

--Get the root object, which has the box2d body applied ...
local hObject       = this.getObject ( )
local hBox2dObject  = object.getParent ( hObject )
 
if( hBox2dObject and maBox2d.hasBody ( hBox2dObject ) )
then
	local nAngleRad = this.nAngle ( ) * 0.0174532925 --google says: 1 degree = 0.0174532925 radian
 
	if( this._bUseObjectScaleAsSize ( ) )
	then
		local nScaleX, nScaleY, nScaleZ = object.getScale ( hObject )
		this.nWidth ( nScaleX )
		this.nHeight ( nScaleY )
	end
 
	if( this._bUseObjectPositionAsOffset ( ) )
	then
		local nPosX, nPosY, nPosZ = object.getTranslation ( hObject, object.kParentSpace )
		this.nCenterX ( nPosX )
		this.nCenterY ( nPosY )
	end
 
	local nCategoryBits  = maBox2d.convertBitsToInt ( this.tCategoryBits ( ) )
	local nMaskBits      = maBox2d.convertBitsToInt ( this.tMaskBits ( ) )
 
	local htShapeDef = hashtable.newInstance ( )
	--Shape def properties:
	hashtable.add ( htShapeDef, "density",              this.nDensity ( ) )
	hashtable.add ( htShapeDef, "friction",             this.nFriction ( ) )
	hashtable.add ( htShapeDef, "isSensor",             this.bIsSensor ( ) )
	hashtable.add ( htShapeDef, "restitution",          this.nRestitution ( ) )
	hashtable.add ( htShapeDef, "filter.categoryBits",  nCategoryBits )
	hashtable.add ( htShapeDef, "filter.maskBits",      nMaskBits )
	hashtable.add ( htShapeDef, "filter.groupIndex",    this.nGroupIndex ( ) )
 
	--Box shape properties:
	hashtable.add ( htShapeDef, "width",    this.nWidth ( ) )
	hashtable.add ( htShapeDef, "height",   this.nHeight ( ) )
	hashtable.add ( htShapeDef, "center.x", this.nCenterX ( ) )
	hashtable.add ( htShapeDef, "center.y", this.nCenterY ( ) )
	hashtable.add ( htShapeDef, "angle",    this.nAngle ( ) )
 
	this.sShapeId ( maBox2d.addShapeBox( hBox2dObject, htShapeDef ) )
end

The above code is actually only used at object creation time. If you have to change single properties during runtime, like maybe modifying the friction value of a shape, there will be API functions for that. You will then write something like maBox2d.setFriction( sShapeId, nFriction ). At this point, I cannot provide a complete list yet … but you see, that it will be more comfortable and safer than the hashtables :)

Next up is plugin compilation for Android!

By the way: I am now using the awesome StoneScript syntax highlighting provided by makkar from Archimmersion! Be sure to check it out if you’re running your own ShiVa related blog.