LibGDX minimal 3D app

10. January 2014

As I mentioned yesterday when talking about how to generate character sprites supporting dynamic inventory, the very first step would be to actually figure out how to do 3D in LibGDX.

 

Well, I did, and thankfully it’s pretty simple.  This isn’t a tutorial by any stretch of the means, but what follows is basically the “minimum” 3D application you can create in LibGDX.  It simple creates a blue cube and rotates the camera around it.  While it isn’t a tutorial, it is heavily documented, so you should be able to figure things out with ease, especially if you’ve already done my LibGDX tutorial series.

 

package com.gamefromscratch;

 

import com.badlogic.gdx.ApplicationListener;

import com.badlogic.gdx.Gdx;

import com.badlogic.gdx.graphics.Color;

import com.badlogic.gdx.graphics.GL10;

import com.badlogic.gdx.graphics.PerspectiveCamera;

import com.badlogic.gdx.graphics.VertexAttributes.Usage;

import com.badlogic.gdx.graphics.g3d.Environment;

import com.badlogic.gdx.graphics.g3d.Material;

import com.badlogic.gdx.graphics.g3d.Model;

import com.badlogic.gdx.graphics.g3d.ModelBatch;

import com.badlogic.gdx.graphics.g3d.ModelInstance;

import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;

import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;

import com.badlogic.gdx.math.Vector3;

 

public class TestApp implements ApplicationListener {

   private PerspectiveCamera camera;

   private ModelBatch modelBatch;

   private Model box;

   private ModelInstance boxInstance;

   private Environment environment;

 

   @Override

   public void create() {

   // Create camera sized to screens width/height with Field of View of 75 degrees

      camera = new PerspectiveCamera(

      75,

      Gdx.graphics.getWidth(),

      Gdx.graphics.getHeight());

 

      // Move the camera 3 units back along the z-axis and look at the origin

      camera.position.set(0f,0f,3f);

      camera.lookAt(0f,0f,0f);

 

      // Near and Far (plane) repesent the minimum and maximum ranges of the camera in, um, units

      camera.near = 0.1f; 

      camera.far = 300.0f;

 

      // A ModelBatch is like a SpriteBatch, just for models.  Use it to batch up geometry for OpenGL

      modelBatch = new ModelBatch();

 

      // A ModelBuilder can be used to build meshes by hand

      ModelBuilder modelBuilder = new ModelBuilder();

 

      // It also has the handy ability to make certain premade shapes, like a Cube

      // We pass in a ColorAttribute, making our cubes diffuse ( aka, color ) red.

      // And let openGL know we are interested in the Position and Normal channels

      box = modelBuilder.createBox(2f, 2f, 2f, 

      new Material(ColorAttribute.createDiffuse(Color.BLUE)),

      Usage.Position | Usage.Normal

      );

 

      // A model holds all of the information about an, um, model, such as vertex data and texture info

      // However, you need an instance to actually render it.  The instance contains all the 

      // positioning information ( and more ).  Remember Model==heavy ModelInstance==Light

      boxInstance = new ModelInstance(box,0,0,0);

 

      // Finally we want some light, or we wont see our color.  The environment gets passed in during

      // the rendering process.  Create one, then create an Ambient ( non-positioned, non-directional ) light.

      environment = new Environment();

      environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1.0f));

   }

 

   @Override

   public void dispose() {

      modelBatch.dispose();

      box.dispose();

   }

 

   @Override

   public void render() {

      // You've seen all this before, just be sure to clear the GL_DEPTH_BUFFER_BIT when working in 3D

      Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

      Gdx.gl.glClearColor(1, 1, 1, 1);

      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

 

      // For some flavor, lets spin our camera around the Y axis by 1 degree each time render is called

      camera.rotateAround(Vector3.Zero, new Vector3(0,1,0),1f);

      // When you change the camera details, you need to call update();

      // Also note, you need to call update() at least once.

      camera.update();

 

      // Like spriteBatch, just with models!  pass in the box Instance and the environment

      modelBatch.begin(camera);

      modelBatch.render(boxInstance, environment);

      modelBatch.end();

   }

 

   @Override

   public void resize(int width, int height) {

   }

 

   @Override

   public void pause() {

   }

 

   @Override

   public void resume() {

   }

}

 

And when you run it you should see:

RotatingCube

 

Next up, loading a 3D model from Blender and playing the animations.

 

By the way, although this isn’t a tutorial, if you have a question about something I did feel free to use the comments and I will do my best to answer!

Programming , ,




Creating dynamically equipped characters in 2D and 3D games

9. January 2014

 

Right off the hop for my new game I have to deal with a problem that many games have to deal with but one that could have a huge impact on how my game is structured, so I am going to address it with a proof of concept right up front.   That thing, as the title probably spoils, is character customization.

 

As I said earlier, this is a pretty common problem in games and something you see questions about quite often if you hang around game development forums for any period of time.  So what do I mean with character customization?  Ever played Diablo or a similar title?  When you change your gear in Diablo, your character is updated on screen.  This behaviour is pretty much expected in this day and age… personally I really hate when I am playing an RPG and the weapon I equip has no affect on my avatar.  That said, it is certainly a non-trivial problem and one that could have a pretty big impact on your game.

 

Let’s start by taking a look at the various options… at least the ones I can think of.

 

2D - Pre-render all possible configurations

Of course, brute forcing it is always a possibility and in a few cases may actually be ideal.  Basically for a 2D game, you simply create every combination of sprite for every combination of equipment.  So if you have an animation sequence for “walk_left” and “walk_right”, you would have to create a version for each possible equipable item.  So you might have “walk_left_with_axe” and “walk_left_with_sword” sprite sheets for the different weapon combinations.  If you are rendering your spritesheets automatically from a 3D application, this could actually be the easiest approach.  Of course there are some seriously massive downsides to this approach.  Memory usage quickly becomes outrageous.  For each piece of equipment you double the size of your original sprite sheet for example.  Once you start adding more and more options, this approach quickly becomes unfeasible.  If you only have a very small amount of customization ( two or three weapon options ), this approach may appeal due to it’s simplicity.  With anything more than a couple combinations though, this approach is pretty much useless.

 

2D – Pre-rendered spritesheet + pre-rendered weapon animation frames.

You can use a regular sprite sheet and simply keep the weapons separate.  So basically you have two separate sprite sheets, one of the character without weapons, one with just the weapons.  Then at run-time you combine them.   Consider these two sprite sheets ( taken from a StackOverflow question ).

Animated Character:

Player SpriteSheet

Weapon:

Weapon SpriteSheet

Then at run-time you combine the two sprites together depending on what is equipped.

In fact, if your weapon isn’t animated ( as in, it doesn’t move, like a sword, as opposed to a weapon like a whip or nunchucks ) you can get by using a hard point system, where you define the mount position of the weapon ( for example, the players right hand ) and orientation.  Basically for each frame of animation, you provide an x and y coordinate as well as rotation amount that represents where and how the weapon should be mounted.  Then at runtime you combine the two sprites, positioning and rotating the weapon sprite relative to the mount/hardpoint. This way, you only need a single image per equipable weapon.

This approach has a couple downsides.  First, you need a separate data file to hold mount point data and possibly a tool to generate it.  Second, it makes bounding/hit detection more difficult as the mounted weapon may exceed the dimensions of the originating sprite.  Third, you need to deal with z-depth issues, you cant simply paste the weapon on top of the sprite, as what happens when the character is walking right but carrying the weapon in the left hand?  To handle this your workflow needs to basically be: render obscured weapons, render character sprite, render foreground weapons.  Finally, weapons with their own animation, like a whip snapping, would require even more effort.

All told, this is probably the easiest while still flexible system for 2D animation.  It requires a bit more work than simply brute forcing your sprite sheets, but the payoffs in reduced size and complexity are well worth it, especially if you have more than a couple items.  One other big win about this approach is then weapons can be re-used across different sprites, a huge win!  Then you can model a single sword and it will be available to all sprite sheets with hard/mount point information available.

 

3D - Bone/hardpoint system

If your game is pure 3D this is often the way you want to go.  If you character is a knight for example, in your 3D modelling application you create named hard points in your model, either as bones or as an object of some form such as a null object, point or invisible box.  Then at runtime you bind other models to those locations.  So for example, if you equip a long sword and shield, your code searches for the “right_arm_shield” bone and uses it as the parent object for a shield 3D model.  Then you bind a sword to say… the “left_arm_hand” bone.  You can see an example of this behaviour in this UDK example.

This approach is basically identical to the last 2D method mentioned except in 3D and that you dont generally need to define your own hardpoint system, as most 3D packages and engines already support it out of the box, either by binding to a bone or by accessing and parenting 3D objects to named locations.  Like working in 2D, then your weapon and character are two separate entities, like so:

3P_Weapon_Attach.JPG

One of the major advantages of 3D over 2D is flexibility.  Most animations in 2D need to be pre-calculated, while in 3D you have a great deal more flexibility with the computer filling in the blanks for you.  Unlike in 2D, additional 3D animations add very little to the data file sizes.

 

2D Bone based animation

One of the major downsides to 2D animation is, as you add more animations, it takes up more space.  People have been working on solutions, one of the most common of which is 2D bone animation.  How exactly can you use bones on a 2D image file you ask?  That’s a good question!  In 3D, its pretty simple… the motion/influence of the bone transforms the attached geometry.  In 2D, there is no geometry, so how can you do this?

Well, the simple version is, you cut your bitmap or vector image up into body parts.  So instead of a single image of an animated character, you instead of an image for the foot, lower leg, upper leg, torso, waist, shoulder, left upper arm, etc…  Then you can draw 2D bones that control the animation of this hierarchy of images.  There are applications/libraries that support this, making it mostly transparent for you.  Here is one such example from Spine from Esoteric Software.

 

The character:

Creating Bones

 

It looks like a single image ( and you can see a couple bones being drawn in the foot/leg area ), but it is actually composed of a few dozen source images:

Set Parent

 

Basically the workflow goes, you create your traditional image in your regular art program, except instead of creating a single image, you create dozens of hopefully seamless ones representing each body part.  You stitch them together, setting the drawing order ( what’s covering what ), then define bones to create animations.  This has the double advantage of massively decreasing space requirements while simplifying the animation process.  The downsides are creating your initial image are a great deal more difficult and of course, you need to buy ( or roll your own ) bone system, such as Spine. 

 

Spine is not the only option, there is also Spriter from BrashSoftware and probably others I am unaware of.  Both of these are commercial packages, but then, both are dirt cheap ( IMHO ) with price tags between $25 and $250 dollars, with $60 and $25 dollars being the norm with each package respectively.

The nice thing about taking this segmented bone approach… you basically get the ability to swap components or bind weapons for free.

 

2.5D hybrid approach

2 1/2D is becoming more and more popular these days.  Essentially a 2.5D game is a 3D game with 2D assets ( such as 2D sprites over a 3D background ), but more commonly its 3D sprites over a 2D background.  How does this work?

Well there are two basic approaches.  You model, texture, animate your 3D model as per normal.  Any character customization you perform you would do as if you were working in full 3D.  Now is where the two different approaches vary, depending on performance, the hardware you are running on, etc…  You can either generate a sprite sheet dynamically.  Essentially when your character is loaded or you change equipment, in the background your game renders a sprite sheet of your updated character containing every animation frame, then you use this sheet as if it was a normal 2D sprite sheet.  The other option is, you render your character each time it moves.  Creating a spritesheet consumes more memory, but very little GPU or CPU demand.  Generating your characters current animation frame dynamically on the other hand, takes almost no memory, but at a much higher CPU/GPU burden.  Which is right for your game is obviously up to you.

Of course the 2.5D approach certainly has it’s share of problems too.  First off, it is the most complicated of the bunch.  Second, you cant do any “by hand” calculations on your generated sprite/spritesheets, as they are now being rendered dynamically.  This means no ability to create pre-calculated bounding volumes for example.  On the other hand, the 2.5D approach gives you most of the advantages of 3D ( compact animation data structure, easy programmable modification, ability to alter texture mapping, lighting effects, etc… ) that 3D does, but with the simplicity of dealing with a traditional 2D world.

One animation to rule them all!

You may be thinking WAHOO… I can cut my work way down now!  All I need to do is animate an attack sequence and then I can simply substitute the different weapon images.  Not so fast I am sorry to say.  Think about it this way…  are the images for swinging an AXE and a Sword the same?  What about a pole arm?  You are still going to have several different animations to support different classes of weapons, but these techniques will still vastly reduce the amount of data and work required while allowing a ton of customization.

 

 

So then, what am I going to do?

So, which approach am I taking?  Ultimately I am going to require a great deal of customization, going far beyond the examples described above.  One such example scenario is a Mech with a customizable left weapon… this weapon could be an arm holding a gun, a turret containing one or more guns, or possibly even a large pack of rockets.  Each “weapon” could have it’s own animation set.  Oh, and I am much more proficient in 3D than 2D.  On the other hand, the game itself is a 2D game and I would prefer to keep it that way for the simplicity that brings in a number of areas ( level creation tools, path-finding and other AI tasks, physics, cheating at art, etc… ).  As a result, I am going to try the 2.5D approach, where the mech is created and animated in 3D, dynamically equipped, then rendered to 2D.  I am going to try both pre-rendering sprite sheets and dynamically rendering a single frame and see what the overhead of each is.  I actually have the compounded issue of dealing with enemy characters that are also dynamically generated, so any impact will be magnified many time over!

If I can’t get adequate performance from each approach, I may be forced to go full 3D and emulating a 2D look.  As I said, it’s one of those things that can have a major impact on your game!  That said, I am pretty confident performance wont be too much of a factor.  My immediate take away task list is:

  • get a LibGDX project set up that can load and render an textured, animated 3D model exported from Blender. I am pretty new to 3D in LibGDX ( okay… completely new to it )
  • bind another model to a bone on this model ( aka, a sword to a hand bone )
  • try to dynamically generate a sprite sheet of the character’s complete animation.  Measure resulting memory usage and time required to generate sheet
  • re-architect the code to instead generate each frame of animation per frame on demand.
  • scale both scenarios up to include multiple instances and see results on performance
  • measure performance impact of each approach on both desktop and mobile.

 

The outcome of this experiment will ultimately decide what approach I take.  Stay tuned if that process sounds interesting!

 

Oh, and of course, these are only the scenarios I could come up with for dealing with dynamically equipped characters, if you have another idea, please share it!

Design, Programming , , ,




Free on screen controller graphics pack released

9. January 2014

Want to create an on-screen controller for your mobile game?  That means providing all the graphics that make up the controller, meaning drawing a gamepad, buttons, etc.  Fortunately user KenNL has released just such a package over on Reddit.

 

The quality is quite good, as you can see:

NewImage

 

The package contains 50 different controls in 8 different styles and are available in PNG, SWF and SVG formats under the creative commons 0 open source license.

 

Very cool work.  You can download the package over at OpenGameArt.  If you like the artists work, follow him on Twitter or Facebook.  Do people still use Facebook? ;)  The author is worth following, as he has released a number of high quality 2D art sets for completely free.




New Year, New Project! Or perhaps more accurately, a very old delayed project

7. January 2014

 

So a new year is upon us; happy belated New Year everyone!

Newyear

 

I figure now is as good of a time as any to get off my arse and work on something I have long been delaying… a complete game.  If you go wayyyyyy back in the annals of GameFromScratch.com history, you will find this post.  My original intention with GameFromScratch.com was to document, well… creating a game from scratch!  Somewhere along the way I sort of wandered off course into writing tutorials and books about game programming in general.  Don’t get me wrong, I absolutely loved the adventure and will continue to create future tutorial series ( and probably books ) as I go forward, but I also intend to go ahead and create that game I was talking about!

 

Funny enough, of the thousands and thousands of game ideas in my head, nearly three years later it’s still the same game that interests me.  I will flesh it out in a bit more detail in later posts.

 

What does this mean for GameFromScratch.com?

 

Well, first the negatives…  there probably won’t be any massive tutorial series like the C++, LibGDX, MOAI or Blender ones, at least for the next few months.  Truth of the matter is, each of these series when put into page form are about equivalent to a 200+ page book!  In fact, I am in the process of turning one of those series into a book and these page count is pushing 300 once converted.  Creating these series represents a massive investment of time and that time won’t be available while working on a game title.

 

That said, I will still continue to do smaller ( say… one to four part ) tutorials on a variety of subjects, it’s just the massive series that I won’t be undertaking.  I will also continue to do in-depth reviews/previews of shiny new gaming technologies like I have in the past.  Of course, I will also be cherry picking the news I think will be of most interest to indie developers.  There are a couple of new site ideas I have as well that hopefully you will find interesting. So, at the end of the day, things won’t be all that different around these here parts.

 

Now the good change, from this point on you are going to have access to a developer blog that goes MUCH MUCH MUCH deeper than almost any I can think of on the net.  I am ultimately creating a product I intend to sell at the end, so I am not going to be working completely open source ( at least, not before release… may open up source code and assets after the fact ).  I am however going to be as open as I possibly can be.  In fact, I’d like to keep a HTML5 playable version of the game available here as I develop!  ( If HTML doesn’t end up being a complete pain in the ass that is…  big if that! ).  So, what’s keeping people from just ripping off my ideas, code, art style etc… and essentially cloning my game?  Nothing I suppose, except karma.  Should your house be visited by a plague of locusts… totally wasn’t me!

 

Along the way, I intend to show behind the curtain for just about everything…  share the more interesting code I develop, show the design process, works in progress, etc…  I will also be documenting all the business process behind the game too, the stuff most of us ignore ( and possibly hate! ).  Things like submitting to the app stores, setting up servers, promoting the product through various means ( ewwwwww ) maybe even topics like dealing with DLC or hosting ads.

 

In many ways, it will be a lot like the site currently is.  The major difference is, the topics I will be focused on in my writing are going to be topics I am actually facing in the real world.  The other big difference is going to be the depth of coverage.  For example, one immediate problem I am going to have to work on is rendering 3D models in realtime for a 2.5D game.  I intend to share the code for how I accomplish this and maybe even a bit of an explanation.  It will not however be a tutorial, so I won’t be going into detail about how I do things… of course, the comments are always available should you have a question.

 

If I pull this off, hopefully it will result in a very clear, detailed, hopefully interesting insight into the entire game development process, from concept to shipping ( hopefully money making! ) product!  Oh and hopefully it will result in one damned fun game too.

 

Hope you enjoy the ride.  Oh, and if there are any shorter tutorial series you are interested in, please let me know!




Leadwerks “indie edition” Game Engine added to steam for $99

6. January 2014

 

It started with GameMaker, now it seems more and more game development packages are being added to Steam, generally at vastly discounted prices.  Today we see Leadwerks Game Engine: Indie Edition has been added for $99.  On the website, Leadwerks is $199.  The question is, what is the limitiation of “indie edition”?  That’s the million dollar question.  It’s not a distribution thing, as they say in the Steam entry:

Your games you make are yours. Yours to play, yours to sell, yours to give away, and do as you please. You will never be charged royalties for any game you make in Leadwerks.

 

Anyways… about Leadwerks

Image Source

 

Leadwerks is a powerful and easy-to-use game engine for building any kind of 3D game. With a rapid development pipeline, Lua script integration, and plenty of learning materials, Leadwerks is the perfect way to make 3D games that look and feel amazing. Get started today making your own 3D games for Steam.

Key Features

Learn to Make Your Own 3D Games
We provide tons of documentation and video tutorials walking you through the steps to build your own 3D games. Leadwerks is the perfect pathway to go from total noob to pro game developer.


Advanced Graphics
Leadwerks brings AAA graphics to the masses, with hardware tessellation, geometry shaders, and a deferred renderer with up to 32x MSAA. Our renderer redefines realtime with image quality more like a cg render than real-time games of the past. The use of OpenGL 4.0 provides equivalent graphics to DirectX 11, with cross-platform support across operating systems, for future expansion.


Built-in Level Design Tools
Build game levels from scratch right in our editor with constructive solid geometry. Our tools make it easy to sketch out your design and bring your ideas to life. Anyone can build their own game worlds in Leadwerks, without having to be an expert artist.


Integrated Lua Script Editor
We integrated Lua right into Leadwerks because of its proven track records in hundreds of commercial games including Crysis, World of Warcraft, and Garry's Mod. Lua integrates seamlessly with native code for rapid prototyping and instant control. The built-in debugger lets you pause your game, step through code, and inspect every variable in the program in real-time. Lua is perfect for beginners, and the integrated Just-In-Time (JIT) compiler ensures your games will keep running fast as they grow.


Visual Flowgraph for Advanced Game Mechanics
Our unique visual flowgraph enables designers to set up game mechanics, build interactions, and design advanced scripted sequences, without touching a line of code. The flowgraph system integrates seamlessly with Lua script, allowing script programmers to expose their own functions and add new possibilities for gameplay.

 

Personally, I’ve no experience with Leadwerks, so I can’t comment on what the engine is like.

 

editFound this post, with a bit more details on the limitations of Steam, sorta…

"Leadwerks: Indie Edition" will be launched on Steam January 6th.  This will be on Windows only, with support for Lua scripting.  The following groups will receive a free Steam key to add this product to their Steam account:

  • Leadwerks 3.0 customers who pre-order the upgrade to version 3.1.
  • New customers who pre-order Leadwerks 3.1.
  • All Kickstarter backers who backed Leadwerks for Linux for $49 or more.  (Even if you don't run Windows, hold onto this as the Linux version on Steam will have special features.)

Leadwerks 3.1 for Linux and Windows will be released together next, with the exact release date to be determined.  Leadwerks 3.1 for Mac will follow this, with mobile add-ons for iOS and Android coming last.  (There is no purchase necessary to upgrade the mobile add-ons from Leadwerks 3.0 to Leadwerks 3.1.)

 

Still not completely sure what that means in terms of limitations… does that mean the Indie edition is Lua only and targets only WIndows?  Or just that the Windows version is available?

 

edit2: Found this comment on the difference:

The standard edition is more expensive but includes C++ programming capability. That's important to some people, and for others it's not a big deal. Since we use a just-in-time compiler to make Lua execution fast, Lua alone is perfectly capable of making full games.

So, indie edition has no C++ support…  that’s a bit of a big difference to many people I should imagine.

News, Programming