I am trying to use chipmunk6.x and having problems with it. It is not creating the physics environment. How do I enable the chipmunk6.x physics environment in my project? The space manager system is working well. I hope there is no problem with the chipmunk lib attachment.
I am using the cocos2d old version I just replaced the chipmunk lib.
space = cpSpaceNew();
space->gravity = cpv(0, -100);
//
// rogue shapes
// We have to free them manually
//
// bottom
cpShape *walls_[4];
walls_[0] = cpSegmentShapeNew( space->staticBody, cpv(0,0), cpv(s.width,0), 0.0f);
// top
walls_[1] = cpSegmentShapeNew( space->staticBody, cpv(0,s.height), cpv(s.width,s.height), 0.0f);
// left
walls_[2] = cpSegmentShapeNew( space->staticBody, cpv(0,0), cpv(0,s.height), 0.0f);
// right
walls_[3] = cpSegmentShapeNew( space->staticBody, cpv(s.width,0), cpv(s.width,s.height), 0.0f);
for( int i=0;i<4;i++) {
walls_[i]->e = 1.0f;
walls_[i]->u = 1.0f;
cpSpaceAddStaticShape(space, walls_[i] );
}
You need to be more specific than "It is not creating the physics environment."
The code you pasted otherwise looks fine. What happens with it, and what are you expecting should happen.
Related
I have some problems understanding the proper usage of Graphics.Blit() in Unity
I have a Shader (volumeShaderSecondPass), for debug reasons it simply returns float4(1.0,1.0,1.0,1.0) right now.
If I use it in a material created in the Editor, I correctly see just a white square if it is applied to a cube:
If I create a material in code and try to use Graphics.Blit() to display it, my whole screen turns white.
void OnRenderImage(RenderTexture src, RenderTexture dst)
{
volumeCam.targetTexture = volumeFirstPassTexture;
volumeCam.backgroundColor = new Color(1.0f, 0f, 0.5f, 0.5f);
volumeCam.clearFlags = CameraClearFlags.Color;
volumeCam.cullingMask = 1 << LayerMask.NameToLayer("Volume");
volumeCam.RenderWithShader(volumeShaderFirstPass, null);
volumeMat = new Material(volumeShaderSecondPass);
volumeMat.hideFlags = HideFlags.DontSave;
volumeMat.SetTexture("_Texture3D", data);
Graphics.Blit(volumeFirstPassTexture, dst, volumeMat);
}
I am trying to make a Scrabble word game using a fixed camera, but I have a simple issue.
I add some boxes as game objects and the number of these boxes is the length of the word so if the word is "Fish" we will add 4 boxes dynamically. I did that successfully, but I can't center those boxes in the screen. I tried to add those game objects as children under another game object, then center the parent, but with no effect.
This is my code:
void Start () {
for (int i=0; i<word.Length; i++) {
GameObject LetterSpaceObj = Instantiate(Resources.Load ("LetterSpace", typeof(GameObject))) as GameObject;
LetterSpaceObj.transform.parent = gameObject.transform;
LetterSpaceObj.transform.localPosition = new Vector2(i*1.5f,0.0f);
LetterSpaceObj.name = "LetterSpace-"+count.ToString();
count++;
}
gameObject.transform.position = new Vector2 (0.0f, 0.0f);
}
This image shows you the idea:
I believe your code is working, but the problem is that your first letter is located at your parent object, and then every letter from then on is added to the right. That means that when you center the parent object what you are doing is putting the first letter in the center of the screen.
If you run the game and use the scene view to look at where the parent object is this can confirm this. What you can do instead is that rather than placing the parent at the center of the screen, offset it by an amount equal to the length of the word.
gameObject.transform.position = new Vector2 (-(word.Length / 2.0f) * 1.5f, 0.0f);
You might also want to consider changing some of those constants, such as the 1.5f into variables with names like LetterSize or basing it off the actual prefab so that way any future changes will work automatically.
This is the last solution after some edits to fix this issue.
GameObject LetterSpaceObjRow;
int count = 1;
string word = "Father";
float ObjectXPos;
float LocalScaleX;
void Start () {
for (int i=0; i<word.Length; i++) {
GameObject LetterSpaceObj = Instantiate(Resources.Load ("LetterSpace", typeof(GameObject))) as GameObject;
LocalScaleX = LetterSpaceObj.transform.localScale.x;
ObjectXPos = i*(LocalScaleX+(LocalScaleX/2));
LetterSpaceObj.transform.parent = gameObject.transform;
LetterSpaceObj.transform.localPosition = new Vector2(ObjectXPos,0.0f);
LetterSpaceObj.name = "LetterSpace-"+count.ToString();
count++;
}
gameObject.transform.position = new Vector2 (-(ObjectXPos/2.0f) , 0.0f);
}
I've uppgraded to PhysX 3.2 and have been struggling for days to have my test box moved by gravity but it simply won't to it.
I've followed the PhysX documentation but implemented it in my way. It's pretty much a default setup:
physx::PxSceneDesc sceneDesc = physx::PxSceneDesc((physx::PxTolerancesScale()));
sceneDesc.gravity = physx::PxVec3(0.0f, -9.8f, 0.0f);
if(!sceneDesc.cpuDispatcher)
{
physx::PxDefaultCpuDispatcher* mCpuDispatcher = physx::PxDefaultCpuDispatcherCreate(4);
if(!mCpuDispatcher)
LOG("PxDefaultCpuDispatcherCreate failed!");
sceneDesc.cpuDispatcher = mCpuDispatcher;
}
if(!sceneDesc.filterShader)
sceneDesc.filterShader = &physx::PxDefaultSimulationFilterShader;
physxScene = physMgr->getSDK()->createScene(sceneDesc);
Creating the dynamic actor:
PxRigidDynamic* body = mPxSDK->createRigidDynamic(Convert::toPxTransform(transform));
PxRigidBodyExt::updateMassAndInertia(*body, 1.0f);
mPxScene->addActor(*body);
Add the box shape:
PxBoxGeometry geometry = PxBoxGeometry(Convert::toPxVector3(size));
if(geometry.isValid())
{
PxMaterial* material = api->createMaterial(0.5f, 0.5f, 0.1f);
PxShape* shape = createShape(actor, geometry, material);
PxRigidBodyExt::updateMassAndInertia(*body, 33.0f);
}
Simulating the scene as:
float elapsedTime = float((float)mTime.getElapsedTime() / 1000.0f);
mAccumulator += elapsedTime;
if(mAccumulator < mStepSize)
{
return;
}
else
{
mAccumulator -= mStepSize;
mPxScene->simulate(mStepSize);
mDynamicBodySys->updateGameObjectPositions();
mPxScene->fetchResults(true);
mTime.restart();
}
When I look into the Visual Debugger I can see the box and the frame count increasing. But it's not moving. The actor's and the box shape seem to have the correct propeties. LinearVelocity is increasing in negative Y axis, its mass is 33 etc. But the pose is still zero/identity. What am I missing?
Solved. The error was in my own logic. There was a sync logic problem where PhysX was trying to update my graphics while in the same time my positioning logic was telling PhysX to update with its previous position. So it got stuck and appeared to never be simulated.
I am trying to make a game in Cocos 2D & Box 2D
but when i try to put a "setAsEdge" it just said me that the function dosn't exist
my code:
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// bottom
groundBox.SetAsEdge(b2Vec2(0,FLOOR_HEIGTH/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,FLOOR_HEIGTH/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// left
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.SetAsEdge(b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
Error: No member named 'SetAsEdge' in 'b2PolygonShape'. I am using Cocos 2D 1.0.1 Stable
Thanks to everyone
You are working with Box2D 2.2.1 whose API has changed compared to the Box2D version in Cocos2D v1.0.1 (uses Box2D v2.1.2). I take it you're using Cocos2D 2.0 alpha? If so, it supposedly comes with updated Xcode project templates, so you might want to install these and create a project based on the new templates.
Here's how I set the border bounding box by using a body with 4 shapes attached to it:
// for the screenBorder body we'll need these values
CGSize screenSize = [CCDirector sharedDirector].winSize;
float widthInMeters = screenSize.width / PTM_RATIO;
float heightInMeters = screenSize.height / PTM_RATIO;
b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters);
// Define the static container body, which will provide the collisions at screen borders.
b2BodyDef screenBorderDef;
screenBorderDef.position.Set(0, 0);
b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
b2EdgeShape screenBorderShape;
// Create fixtures for the four borders (the border shape is re-used)
screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(lowerRightCorner, upperRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperRightCorner, upperLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
Alternatively, get Kobold2D Preview 5 (available by tomorrow) and have a look at the Physics-Box2D template project. I made all the necessary changes to run a Box2D v2.2.1 app, including the changes needed for the Box2D debug drawing class. I'm also hosting the latest Box2D API reference.
Use b2EdgeShape instead of b2PolygonShape, for example:
b2EdgeShape groundBox;
groundBox.Set( b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO,0));
use
setAsBox(0,0,b2vec2(0,0),0);
I am trying to turn off gravity on one of my bodies. I have used the bodyDef.gravityScale = 0.0f but I am having no luck. Here u can look at my code below. Please help.
b2BodyDef monkey1BodyDef;
monkey1BodyDef.position.Set(0, 200/PTM_RATIO);
monkey1BodyDef.type = b2_dynamicBody;
monkey1BodyDef.userData = monkey1;
monkey1BodyDef.bullet = true;
monkey1BodyDef.gravityScale = 0.0f; //Why doesn't this work I get an error that says no member named 'gravityScale' in 'b2BodyDef'
b2Body *monkey1Body = world->CreateBody(&monkey1BodyDef);
I've hit this problem too. After a little digging I've found that the stable Cocos2D builds don't include recent versions of Box2D, so gravityScale is missing from b2BodyDef. That explains the discrepancy with the Box2D documentation.
There are workarounds, but I've opted to update my Box2D to 2.2.1 (currently the latest). In doing that I encountered the following issues (with solutions):
The b2PolygonShape.SetAsEdge method no longer exists. If you're using that to define screen boundaries you'll need to use something like "myPolygonShape.Set(lowerLeftCorner, lowerRightCorner);" for each screen edge. There's an excellent discussion of this at Programmers' Goodies.
b2DebugDraw has been superseded by b2Draw. Just replace any calls to b2DebugDraw with b2Draw and you should be set. For example, if, like me, you're using the Cocos2D Box2D template, you'll need to replace this:
// Debug Draw functions
m_debugDraw = new GLESDebugDraw( PTM_RATIO );
_world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2DebugDraw::e_shapeBit;
flags += b2DebugDraw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
with this:
// Debug Draw functions
m_debugDraw = new GLESDebugDraw( PTM_RATIO );
_world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
flags += b2Draw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
b2Transform has different attribute names for position and rotation. For example, myTransform.position is now myTransform.p (but is still a b2Vec2). myTransform.R, which was defined as b2Mat22, has been replaced with myTransform.q, defined as b2Rot. Again, if you're using the Cocos2D Box2D template, replace the following in GLES-Render.mm:
void GLESDebugDraw::DrawTransform(const b2Transform& xf)
{
b2Vec2 p1 = xf.position, p2;
const float32 k_axisScale = 0.4f;
p2 = p1 + k_axisScale * xf.R.col1;
DrawSegment(p1,p2,b2Color(1,0,0));
p2 = p1 + k_axisScale * xf.R.col2;
DrawSegment(p1,p2,b2Color(0,1,0));
}
…with:
void GLESDebugDraw::DrawTransform(const b2Transform& xf)
{
b2Vec2 p1 = xf.p, p2;
const float32 k_axisScale = 0.4f;
p2 = p1 + k_axisScale * xf.q.GetXAxis();
DrawSegment(p1,p2,b2Color(1,0,0));
p2 = p1 + k_axisScale * xf.q.GetXAxis();
DrawSegment(p1,p2,b2Color(0,1,0));
}
I hope this helps!
Because no member named 'gravityScale' exists in 'b2BodyDef' :(
documentation is outdated compared with the code
Change gravity definition of world, coz it's world, that have gravity, As:
b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);
World is b2World
If body.setGravityScale(0); doesn't work, you can use it with body.setAwake(false); at second line.