Libgdx collision detection with Physics Body Editor - libgdx

I have recently started developing with Libgdx. Now, I'm looking at collision detection for custom shapes. In my case, I want to detect the collision of a shark with other objects. As a shark is a custom shape, I used the Physics Body Editor (https://code.google.com/p/box2d-editor/downloads/detail?name=physics-body-editor-2.9.2.zip&can=2&q=) to transform the shape into a json format.
I already have the code for the image drawing of the sharks and other stuff, but now I have no clue of how to implement the detection of collission with the json file. The tutorial on the phycics body editor website uses a different approach than I do.
Right now, I am drawing my shark like this in my render method:
batcher.draw(sharkAnimation, shark.getX(),
shark.getY(), shark.getWidth(), shark.getHeight());
sharkAnimation is a TextureRegion, and shark is an object with an X, Y, width and height.
The sharks width and height are variable, but maintain the same ratio's.
I already got the bodyeditor for libgdx, and I'm experimenting with the following code, but honestly I have no clue of how I should handle this.
BodyEditorLoader loader = new BodyEditorLoader(
Gdx.files.internal("data/shark.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.position.set(0, 0);
bd.type = BodyType.DynamicBody;
// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;
loader.attachFixture(????, ????, ???, ????);
Help is greatly apreciated.

You need to create a body, and then use the loader the attach the fixture you created in the editor to that body.
Body body = getWorld().createBody(bd);
loader.attachFixture(body, name, fd, scale);
The name is whatever you called it in the physics editor. Scale is how much you want to scale it by from the default size. Just use 1 if you don't want to change it.

Related

Programming a Custom Ease

Trying to create a dynamic wheelSpin with an easeInOut effect, but I want to manipulate the strength of the in and out more precisely. It will have a much longer ease out for example. AS3 Tween locks you into some simple presets but doesn't give me the control I need. I haven't found any tutorials on how to make one. The only thing I've seen for this is Greensock Custom Ease, but I'm trying to either create my own or use something free that doesn't have license of use issues first.
I have an idea to create a movie clip where I tween a simple object using bezier control, then making a call to the tweened property each frame and store it in a variable. Then I make a function for the object I want to manipulate to apply this change over a time relevant to that movies length of time. It seems like a functional hack...but rather clunky. I'd rather keep this code driven.
You can use my tweening class ru.delimiter.math.TweenALot, it depends on ru.delimiter.events.Chronos from the same repo, you can grab it or you can change TweenALot to use ENTER_FRAME instead.
The class is pretty old so please don't judge me for its style.
The usage:
import ru.delimiter.math.TweenALot;
var aTween:TweenALot = new TweenALot;
// The tweening target.
aTween.target = this;
// The destination properties.
aTween.properties = {x:100, y:100, alpha:0};
// The tweening duration, in milliseconds.
aTween.duration = 3000;
// Easing.
aTween.easingFunction = TweenALot.easeInOut;
// Pass the complete handler and start tweening.
aTween.start(onComplete);
function onComplete():void
{
trace("I have disappeared!");
// Destroy and release the tween instance.
aTween.destroy();
aTween = null;
}
Then, at the bottom of the class, there are a number of easing functions, I believe I did them to be compatible with something, just don't remember what exactly. Thus, you can create your own easing functions, you don't even need to add them to the class, just assign them to easingFunction property of the tween.
function weirdEasing(t:Number, b:Number, c:Number, d:Number):Number
{
var aProgress:Number = Math.min(1, t / d);
if (aProgress <= 0.5) return b;
return b + c * Math.pow((aProgress - 0.5) * 2, 2);
}
An alternative nice tween to give a slow organic tween out effect, slower than most exponential tweens, is using plain and simple zeno's paradox:
obj.x += (targetX - obj.x) / 0.5;
Just play with parameters, or add finer control. It's fun. You can add a check to se when distance is small enough to stop motion.

move dynamic body object in box2d when touchbegin

I am creating a game like bounce ball using box2d in cocos2d-x. I created a dynamic body object and i want it to move when touch began. I am using the following code to move the dynamic body but it is not moving. Please anyone could help me to solve the problem.
bullet2=CCSprite::create("block.png");
bullet2->setPosition(ccp(2740, 1220));
this->addChild(bullet2,0);
ballBodyDefB.type=b2_dynamicBody;
ballBodyDefB.position.Set(2740/PTM_RATIO, 1170/PTM_RATIO);
ballBodyDefB.userData=bullet2;
ballBodyDefB.gravityScale=0;
_bullet=_world->CreateBody(&ballBodyDefB);
b2PolygonShape bulletshape;
bulletshape.SetAsBox(bullet2->getContentSize().width/PTM_RATIO/2,
bullet2->getContentSize().height/PTM_RATIO/2);
b2FixtureDef b_bullet ;
b_bullet.shape = &bulletshape;
b_bullet.density = 1.0f;
b_bullet.friction = 0.1f;
b_bullet.restitution = 0.0;
_bullet->CreateFixture(&b_bullet);
CCTouchbegan:
b2Vec2 force = b2Vec2(0, -450);
_block->ApplyLinearImpulse(force, _block->GetPosition());
Hi moving the sprite which follow b2body is easy. You need to attach Sprite to the b2body.userData and in box2d world step synchronize the position and rotation of your sprite with your b2body.
I can't answer directly your question, please post your code and what version of cocos2d-x you have for more details:
Check if your code contain code about: (depend on cocos2d-x version, you use CCSprite which is deprecated in 3.2, we use Sprite only anyway)
box2d world (you already have _world)
_world step iteration
syncing sprite position with b2body
Anyway you can find more details at these links:
http://www.cocos2d-x.org/wiki/Box2D and http://www.cocos2d-x.org/wiki/Getting_Started_with_Cocos2d-x

FloodFill in AS3

I have been making a basic painting application similar to MS-Paint with basic paint, eraser and fill tools. It's this last one that's giving me some trouble.
I'm pretty new to using BitmapData but the idea is that when the user clicks the board, it triggers the startFloodFill method. This is shown below:
public static function startFloodFill(e:MouseEvent):void
{
trace("FLOODFILL");
var boardRef:MovieClip = e.currentTarget.parent.board; //Creates a reference to the board
var boardData:BitmapData = new BitmapData(boardRef.width, boardRef.height); //Creates a new BitmapData with the same size as boardRef
boardData.floodFill(e.localX, e.localY, 0x00CCCCCC); //Applies the FloodFill
boardData.draw(boardRef); //Saves the boardRef as bitmapData
boardRef.bitmapData = boardData; //Updates the board
boardRef.parent.addChild(boardRef);
}
Can anybody tell me what I've done wrong here? When I click, the board does not change. I expected the FloodFill to fill the entire bitmap with the chosen colour as the board is blank when I click.
I also tried replacing the last two lines with:
boardRef.addChild(new Bitmap(boardData) ); //Updates the board
Thanks
The problem is that you first use the floodFill, and then use the draw method, which actually fills the BitmapData with whatever param you give it - in your case it's the boardRef.
You should first draw the boardRef into the boardData, and then use floodFill. At the end, you need to create new Bitmap and display it.
Now you are setting the bitmapData of a MovieClip?! Don't know what you wanted, but you just need to add new child (new Bitmap)

How to replace bitmapData.copyPixels() with bitmapData.draw()

I'm trying to draw a level of my game using a tileset and a xml with the info. It works using copyPixels, but some tiles need to be flipped before they are drawn, so for that I need to use draw() instead of copyPixels(), but I can't get it to work. This is how I use copyPixes:
rectangleSelection = (desiredTile.x, desiredTile.y, tileWidth, tileHeight);
bmpData.copyPixels(tileset.bitmapData, rectangleSelection, new Point(pt.x, pt.y));
//pt.x and pt.y = tile location to be drawn().
How can I do the same thing using the bitmapData.draw() method? I just can't make it work.

Surface in box2das3

I'm using box2dflash for as3 and i need to create curvy ground like this.
Facts:
I can't create one solid object as far as I know because box2d support convex objects;
I didn't find any line-based objects.
Question:
Is there any better way than doing this the way shown on image below?
I couldn't say for sure, but I think that is the best way of doing it.
There is some code on this article for doing a similar thing:
Terrain like tiny wings
You will want to look at the "drawHill" function (Line 91). Sorry I can't be of much more help, I have limited experience with box2D.
I've discovered that you actually can create line.
var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
groundFixtureDef.density = 1;
var someShape: b2PolygonShape = new b2PolygonShape();
someShape.SetAsArray( new Array(new b2Vec2(-3,0),new b2Vec2(2,0)), 2);
groundFixtureDef.shape = someShape;
This would create a simple line. But if you add more points, it'll form convex shape.