Home > other >  Learning cocos2dx physics engine encountered a problem
Learning cocos2dx physics engine encountered a problem

Time:09-25

Run the tutorial example program, click on any screen produces a small ball, free fall, the screen is the borders,
Then the question arises, after the ball fell to place, some would be disorderly jump, sometimes jump up slowly, sometimes very fast, even faster to direct the borders,
Baidu for a long time didn't find a solution, can you tell me is what problem,
Attached source code,
The HelloWorld. H
 # # ifndef __HELLOWORLD_SCENE_H__ 
# define __HELLOWORLD_SCENE_H__

# include "cocos2d. H"

The class HelloWorld: public cocos2d: : Layer
{
Public:
//there 's no' id 'in CPP, so we how returning the class instance pointer
The static cocos2d: : Scene * createScene ();

//Here 's a difference. The Method' init 'in cocos2d - x returns a bool, home returning' id 'in cocos2d - iphone
Virtual bool init ();

//a selector that callback
Void menuCloseCallback (cocos2d: : Ref * pSender);

//implement the "static the create ()" method manually
CREATE_FUNC (HelloWorld);
};

# endif//__HELLOWORLD_SCENE_H__

The HelloWorld. CPP
 # include "HelloWorldScene. H" 

USING_NS_CC;

Scene * HelloWorld: : createScene ()
{
//'scene' is an autorelease object
Auto scene=scene: : createWithPhysics ();

Scene - & gt; GetPhysicsWorld () - & gt; SetGravity (Vect (0.0 f to 100.0 f));

Scene - & gt; GetPhysicsWorld () - & gt; SetDebugDrawMask (PhysicsWorld... DEBUGDRAW_ALL);

//'layer' is an autorelease object
Auto layer=HelloWorld: : create ();

//add layer as a child to scene
Scene - & gt; AddChild (layer);

//return the scene
Return the scene;
}

//on "init" you need to initialize your instance
Bool HelloWorld: : init ()
{
//////////////////////////////
//1. Super init first
if ( ! Layer: : init ())
{
return false;
}

The Size visibleSize=Director: : getInstance () - & gt; GetVisibleSize ();

//create a Sprite
Auto edgeSp=Sprite: : create ();
//create a static quadrilateral rigid body, said the screen edges
Auto body=PhysicsBody: : createEdgeBox (visibleSize PHYSICSBODY_MATERIAL_DEFAULT, 3);

EdgeSp - & gt; SetPosition (visibleSize. Width/2, visibleSize height/2);
EdgeSp - & gt; SetPhysicsBody (body);
This - & gt; AddChild (edgeSp);

STD: : vector ImageNames;
ImageNames. Push_back (" also PNG ");
ImageNames. Push_back (". "PNG");

Auto planeListener=EventListenerTouchOneByOne: : create ();
PlaneListener - & gt; OnTouchBegan=[] (Touch Touch * and Event * Event) {return true; };
PlaneListener - & gt; OnTouchEnded=[=] (Touch Touch * and Event * Event)
{
Vec2 touchLocation=touch - & gt; GetLocation ();
Vec2 nodeLocation=this - & gt; ConvertToNodeSpace (touchLocation);

Int rand=random () % 2;
STD: : string imageName=imageNames. Ats (rand);
Sprite * ball=Sprite: : create (imageName);
Ball - & gt; SetPosition (nodeLocation);

Auto body=PhysicsBody: : createCircle (ball - & gt; GetContentSize (). The width/2);
Body - & gt; SetVelocity (Vect (0.0 f to 100.0 f));
PhysicsShape * ps=body - & gt; GetShape (0);
Ps - & gt; SetMoment (0.2);
Ps - & gt; SetMass (1.0 f);
Ps - & gt; SetDensity (0.2 f);
Ps - & gt; SetFriction (0);
Ps - & gt; SetRestitution (0);
Ball - & gt; SetPhysicsBody (body);
This - & gt; AddChild (ball);
};
_eventDispatcher - & gt; AddEventListenerWithSceneGraphPriority (planeListener, this);

return true;
}


Void the HelloWorld: : menuCloseCallback (Ref * pSender)
{
Director: : getInstance () - & gt; The end ();

# if (CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
exit(0);
# endif
}

CodePudding user response:

The problem is frame frequency with the physical simulation step, generally appear in the first initialization interface card frame, if your machine is fast, won't appear,
The solution
1 shut down automatically simulate
Scene - & gt; GetPhysicsWorld () - & gt; SetAutoStep (false);
2 in the update method belongs to add the following content
If (dt & gt; 0.016 f & amp; & Dt & lt; 0.025 f)
{
Scene - & gt; GetPhysicsWorld () - & gt; Step (dt);//dt is method, update the parameters of (flaot dt)
}

In the above content (dt & gt; 0.016 f & amp; & Dt & lt; 0.025 f) said only in the frame frequency is stable in the range of physical simulation,
Concrete frame frequency range can be adjusted according to their demand, the general frame frequency are floating around in 1/60 (0.016), if the float is too big, that card frame, then no physical simulation, thus to prevent the problem of automatic bounce,
  • Related