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
Related
i'm using cocos2dx 3.3 and i setDebugDrawMask looks like below
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
But nothing draw on my screen evenif there has many boxes.
Here is my code snippet for drawing debugNode for physics space. However it is written in js for Cocos2d-x-js, I hope it will help.
function initDebugMode(controller) {
var phDebugNode = cc.PhysicsDebugNode.create(space);
controller.addChild(phDebugNode, 10);
}
create your scene using below code
auto scene = Scene::createWithPhysics();
then go ahead and add features to your scene
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
Hope this helps
I want to try a simple task where if i move a object inside the world and then press a button it should go back to its original position , but its not working , below is the code i am using - the file is here - http://www.fastswf.com/yAnIvBs (when i remove the event listener)
with event listener - http://www.fastswf.com/rpYsIt8
////////========================
stop();
var startXPos:Number = level1WorldObj.box1.x;
var startYPos:Number = level1WorldObj.box1.y;
function areaS(e:Event) {
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
level1WorldObj.box1.removeEventListener(Event.ENTER_FRAME, areaS);
}
but1.addEventListener(MouseEvent.CLICK,nClick3);
function nClick3(event:MouseEvent):void{
level1WorldObj.box1.addEventListener(Event.ENTER_FRAME, areaS);
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
}
/////////////////======================
Now i want to be able to do it many time so i kept the variables that detect the initial x, y as global ...
Here you can see how it behaves in debugdraw mode , strangely only the clip moves not the actual body - http://www.fastswf.com/-Ijkta4
Can some one please guide me here ...
Thanks in advance ...
Jin
The graphics that you see (box1) aren't related to the physical object behind the scenes - you're currently only moving the graphics not the object itself.
You need to use either SetPosition() or SetTransform() on the b2Body of the object
Edit 07/7
As you're using the Box2D World Construction Kit, I took a look at the source code (available here: https://github.com/jesses/wck). The main class seems to be BodyShape (https://raw.githubusercontent.com/jesses/wck/master/wck/BodyShape.as).
Looking through it, you should be able to access the b2Body directly. If it's null (which is probably the source of the TypeError that you're getting, then you haven't called createBody(), which is what actually takes all of your properties as creates the physical object behind the scenes.
Once you have a b2Body, if you want to position it based on the graphics, there's a function syncTransform() to do just that.
You should turn on debugDraw on your World class to make it easier to see what's going on in the background. NOTE: this needs to be done before calling create()
I was able to find solution to this problem , i found the starting point by using this -
trace(level1WorldObj.box1.b2body.GetPosition().x);
trace(level1WorldObj.box1.b2body.GetPosition().y);
then once i had the position manually i took down the coordinates and used the below code ....
level1WorldObj.box1.b2body.SetTransform(new V2(-2, 2),0 );
Thanks #divillysausages for all the help ...
Regards
I am developing a game in cocos2d-x. In that game I am having two sprites, When i click on a sprite one after another it should swap.I don't know the code for that please help.I created the sprite using following code.
CCSprite *splash=CCSprite::create("misc_textur111.jpg");
splash->setPosition(ccp(500,300));
this->addChild(splash,1);
CCSprite *splash1=CCSprite::create("misc_textur222.jpg");
splash1->setPosition(ccp(300,600));
this->addChild(splash,1)
Now I need to swap the position of both the images.please help.
You can do it like:
CCPoint position(splash->getPosition());
splash->setPosition(splash1->getPosition());
splash1->setPosition(position);
I'd suggest, if you can, upgrading to a newer release of Cocos2d-x. Version 2.23 or even better, version 3.v. Dragging sprites around is trivial with the new EventDispatcher You can create a Listener for each Sprite.
Example:
//Create a "one by one" touch event listener (processes one touch at a time)
auto listener1 = EventListenerTouchOneByOne::create();
// When "swallow touches" is true, then returning 'true' from the onTouchBegan method will "swallow" the touch event, preventing other listeners from using it.
listener1->setSwallowTouches(true);
//Trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
//Move the position of current button sprite
target->setPosition(target->getPosition() + touch->getDelta());
};
Check out the Cocos2d-x Wiki: http://cocos2d-x.org/wiki
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.
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.