Wednesday, October 12, 2016

Godot (GDScript) Memory Management

I've been working with the cocos2d-x engine for a while and have gotten used to one of the important features in the engine.. the autorelease().

It's basically a reference counting implementation which automatically deletes any object that is removed from a scene. Unless ofcourse you called a retain on it. So when i was working on DotDot a simple 2D game, i created a crosshair animation everytime the user got a perfect alignment and removed the object once the animation was done. It worked great and for a while i assumed that everything was fine.

I was showing off the game to people on my android device when i started noticing slowdowns once in a while.. and as the score got higher it got worse.. surely the engines' fault i assumed.. but then i thought ill' give Godot the benefit of doubt and checked the documentation about freeing the memory. That is when i discovered that the objects removed from the scene are not actually removed from memory. Basically every time i did a perfect align in the game i was leaking memory. I used the inbuilt profiler to try and figure out which object was getting left out in the memory and it was the cross hair object.

So how do we get around this problem?.. Ideally we should be maintaining a pool of the objects and changing position / re animating the object.. but for the game it wasn't that important. I chose the method of creating / destroying the object instead and for this to work i had to use "queue_free()" call in the "_exit_tree()" which is called when the node is removed from tree.

Hope this helps anyone trying to do something similar, this was definitely a big miss and could become a problem if you miss it :)

Sunday, October 02, 2016

Pixel perfect mouse click on Images in Android Canvas

I was trying to build a sample for some image manipulation tool. The requirement was that the user should be able to pick an image that he pasted on the screen, move it around, scale and rotate it.

The way we would do this using OpenGL is create a depth buffer use different depth value on each of the Quads' used to render the transparent image. So when you do a readPixel from the depth buffer you will get the z value of the sprite if it was under the touch. This is how we wold distinguish between two overlapping transparent images.

The tracking of z value for different Quads is done automatically in OpenGL when you bind the depth buffer to the current frame buffer and start drawing stuff. Now this i know and have used for a while, but how does this work in the Android Canvas drawing world?

Short answer: in the same way
Long answer: 
These are the steps you should follow to get it done:
  • You have to create a separate Bitmap of the same size as your drawing area
  • Set the Bitmap.Config parameter to Bitmap.Config.A_8
  • Bind a canvas to the buffer. 
  • on Touch event, set the clipping rect of the backbuffer canvas to a rect around the touch location.
  • Read the pixel value from the back buffer bitmap. 
  • Compare the value of pixel to see if it matches any of the sprites. If it does then you have a touch on image else its not.
Following is the function for detecting the pixel value from the backbuffer based on touch location.


This should help you get ahead if you are stuck with the same problem :)



Thursday, July 14, 2016

Vehicle Physics (Godot 2D)

I've always been fascinated with the vehicle physics used in games. I played so many racing games / the hill climb games and every single time i would wonder wow that must be really hard to do.

Thanks to the new generation of game development tools, developing physics based games has become almost a second nature to most people. I didn't want to be left out so i gave it a shot..
I tried setting up vehicle physics in GoDot Engine for a 2D vehicle.

Here is what the vehicle scene looks like..

2D vehicle setup in GoDot
As you can see the body collision shape is not right but the rest of the stuff just works. The idea is simple think of what parts move along with the body vs what doesn't.

I was kinda stuck setting up the pinJoint2D in GoDot, it actually clearly takes two nodes under the properties. This makes sure that the wheels are hinged to the DampingSpring2D. The Damping Spring2D takes two physics bodies(Body and the PinJoint2D) and makes it work like shock absorbers.

You can see that under the panel for structure of the node setup.

you can checkout the code at.. github

Sunday, April 24, 2016

Drawing 24/04/2016

Some ragged shooter
Was trying to see if i can draw something quick as it's been a while since ive' done any. Hopefully will be able to get back to doing this regularly.

Saturday, January 30, 2016

CocosBuilder Plugins

I know that cocos builder is old news by now. People have moved on to sexier more advanced tools for getting things done, namely CocosStudio and SpriteBuilder.

I have been vocal about using this editor at work because we have been able to integrate it with our workflow very easily.
1) Ability to create custom classes is just amazing,
2) The animation creation just works.

The one thing i do crib about is that there was no way to get those custom classes that we created for regular items integrated to the editor.

I've tried to get plugins working a while ago and just gave up because it didn't work. Today i was motivated enough to dig deep and get the damn thing working. I was unable to push to the original repo but have forked and have put in the stuff.

Ill try to see if i can extend this to be more useful may be change the renderer to cocos2d-x instead of cocos2d. These are some things ive' been thinking about will have to see where this goes.

Just excited to get plugins work!  :)
CCRotatingSprite plugin sample
If you want to checkout and build then you should point to https://github.com/vkbsb/CocosBuilder
There is still a slight problem of cocos2d-iphone not being available.. worry not.. it's just changed the repo url https://github.com/cocos2d/cocos2d-iphone-classic.git, this is the new one.
Checkout the right version and you are set to go.

Thursday, January 14, 2016

08_15_01_2016

This is a piece I did completely on mi4 using drawchemy, sketchexpress, and adobe draw.

Seems like a good start.. I'm just not liking the imperfect outline because of lack of bezier curve tool like in inkscape on desktop.

Probably just a matter of preference but this seems to have come out well.