I have created a mallet, and made a b2MouseJoint to drag mallet, like this:
if (_playerBuckFixture->TestPoint(locationWorld)) {
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _playerBuckBody;
md.target = locationWorld;
md.collideConnected = true;
md.dampingRatio = 0.0f;
md.frequencyHz = 60.0;
md.maxForce = powl(_playerBuckBody->GetMass()+10,37);
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
_playerBuckBody->SetAwake(true);
}
but when i move my finger fast, mallet use to loose grip from my finger, and follow my finger not as fast as my finger moves.
Please give me good suggestion to improve b2MouseJoint grip.
Thanks in advance.
Touch locations when moving a finger fast can be very far apart. You may need to interpolate between touch location and the previous one.
Related
So, I ask question about my 2D game, actually it is not game, but it is something like poor game. Well i made moving system and now is time to make jump. I made some kind of gravity with tutorial but I don't like it. It is simple and nice but there is problems in that gravity example: 1. You can hold up arrow and "fly" with jump. I tried to fix that and make some kind of limit to jump but it was bad try. Second problem is that when you jump and you release up arrow (jumping key) then my sprite fall very fast and I can't really control that sprite in air.
This is my first game and I want spend time to do that, but now i have problem that i can't solve with my self so if you can help, please do it. I use C++ with CodeBlocks and SFML multimedia library.
So there is little parts of my code.
First there is velocity and gravity configuration:
//gravity & velocity
const float gravity = 1;
int ground = 600; //height of ground
Vector2f velocity(Vector2f(0,0));
float movingSpeed = 0.5f, jumpingSpeed = 2.0f;
Then there is simple when you press button you jump:
if(Keyboard::isKeyPressed(Keyboard::Up)){
skeleton.move(0,velocity.y = -jumpingSpeed);
source.y = Up;
isWalking = true;
}
And there is system what recognize if you jump and this make you fall:
if(skeleton.getPosition().y + skeleton.getScale().y < ground || velocity.y < 0){
velocity.y += gravity;
}else{
skeleton.setPosition(skeleton.getPosition().x, ground - skeleton.getScale().y);
}
So my sprite name is "skeleton". And yeah this is first time with game physics. I'm not very good game programmer so these code can be poor and yeah... if you invent a solution share it.
I have this little spaceship I want to follow and be smoothly about it. So I calculcate my mouse-coordinates and the offset from the last frame, add a yaw (x-coordinate) and a pitch (y-coordinate), then I do a little geometry et voilá I have my new front-vector for the followedObject.
Currently I follow my object like this:
void Camera::update()
{
glm::vec3 objPosition = followedObject->GetPosition();
this->position = objPosition;
this->position.z += 15;
this->camFront = followedObject->getFront();
this->viewMatrix = glm::lookAt(position, position + camFront, camUp);
}
The z-Offset is used so the object is actually before the camera. Now this whole stuff is rather suboptimal. It worked great as long as I only moved the camera around, but now I really need a better solution.
I'd like to always center the followedObject on my screen and when I perform a curve or any direction change I want the camera to follow smoothly (like with a little transition)
Do you have any ideas how I could improve this?
EDIT 2: Problem solved! I can´t promise it will work with different settings, but by putting my block´s body density to 0, the stack of blocks did not fall when new blocks are added.
I´m sorry about the poor title of the question, I´ll explain my problem closer here:
So, I´ve used Box2D and cocos2D to setup a simple project where two boxes stacks on top of each other (I´m planning to expand to 8-10 boxes). Right now, using a friction of 10.0f on each box, the box at the top still moves around a little. If I would add more boxes, the "tower" would fall and I don´t want that.
I want the boxes to use the gravity to move down, but I never ever want them to change there start x-value.
So, how could I prevent my tower of boxes to fall over or prevent my boxes from moving in x-direction?
EDIT: Posting some code
This code creates on of the boxes, the other one just have a different sprite file.
CCSprite *block = [CCSprite spriteWithFile:#"red.png"];
block.position = ccp(200,380);
[self addChild:block];
//Body definition
b2BodyDef blockDef;
blockDef.type = b2_dynamicBody;
blockDef.position.Set(200/PTM_RATIO, 200/PTM_RATIO);
blockDef.userData = block;
b2Body *blockBody = _world->CreateBody(&blockDef);
//Create the shape
b2PolygonShape blockShape;
blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/2, block.contentSize.height/PTM_RATIO/2);
//Fixture defintion
b2FixtureDef blockFixtureDef;
blockFixtureDef.shape = &blockShape;
blockFixtureDef.restitution = 0.0f;
blockFixtureDef.density = 10.0f;
blockFixtureDef.friction = 10.0f;
_redBlockFixture = blockBody->CreateFixture(&blockFixtureDef);
Nothing fancy.
Regards.
You could setup a 2 (1 pixel wide) walls in box2D to the left and right of the block. Here's some sample code for the left wall. To create the right wall, just copy and past the code and change the variable names and the position of the BodyDef.
// Constant you'll need to define
float wallHeight;
// Create wall body
b2BodyDef wallBodyDef;
wallBodyDef.type = b2_dynamicBody;
wallBodyDef.position.Set(200 - block.contentSize.width/PTM_RATIO/2, 0);
b2Body *wallBody = _world->CreateBody(&wallBodyDef);
// Create wall shape
b2PolygonShape wallShape;
wallShape.SetAsBox(1, wallHeight);
// Create shape definition and add to body
b2FixtureDef wallShapeDef;
wallShapeDef.shape = &wallShape;
wallShapeDef.density = 100.0f;
wallShapeDef.friction = 0.0f;
wallShapeDef.restitution = 0.0f;
b2Fixture *wallFixture = wallBody->CreateFixture(&wallShapeDef);
I solved this problem by adjusting the restitution (bounce) of the static surface upon which the blocks are stacked. For example, if the floor has a restitution of .2, a stack of five blocks will look like they are compressing into each other, and eventually topple:
Set the restitution of the floor to 0, and the blocks stay stacked the way you would expect:
I want to build a platform game with cocos2d/Box2D. I use CCFollow to follow the player sprite but CCFollow constantly puts it in the center of the screen. I want CCFollow to follow more naturally, like a human turning a camcorder with an acceptable lag, a small overshoot ...etc.
Here is a method of mine that didn't work: I attached (via a distance joint) a small physics body to the player that doesn't collide with anything, represented by a transparent sprite. I CCFollow'ed the transparent sprite. I was hoping this ghost body would act like a balloon attached to the player, hence a smooth shift in view. The problem is distance joint breaks with too heavy - too light objects. The balloon moves around randomly, and of course, it pulls the player back a little no matter how light it is.
What is a better way of following a moving sprite smoothly?
Try add this to CCActions in cocos2d libs.
-(void) step:(ccTime) dt
{
#define CLAMP(x,y,z) MIN(MAX(x,y),z)
CGPoint pos;
if(boundarySet)
{
// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
if(boundaryFullyCovered) return;
CGPoint tempPos = ccpSub(halfScreenSize, followedNode_.position);
pos = ccp(CLAMP(tempPos.x,leftBoundary,rightBoundary), CLAMP(tempPos.y,bottomBoundary,topBoundary));
}
else {
// pos = ccpSub( halfScreenSize, followedNode_.position );
CCNode *n = (CCNode*)target_;
float s = n.scale;
pos = ccpSub( halfScreenSize, followedNode_.position );
pos.x *= s;
pos.y *= s;
}
CGPoint moveVect;
CGPoint oldPos = [target_ position];
double dist = ccpDistance(pos, oldPos);
if (dist > 1){
moveVect = ccpMult(ccpSub(pos,oldPos),0.05); //0.05 is the smooth constant.
oldPos = ccpAdd(oldPos, moveVect);
[target_ setPosition:oldPos];
}
#undef CLAMP
}
i get this from cocos2d forums.
Perhaps this http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_ease can help you get an "acceleration" effect with CCFollow.
I have bullets in box2d/cocos2d-for-iphone. They are flying fine...but I want to destroy these bullets after they traveld a certain distance. for example after a bullet "flew" 480px it should be removed.
How can I achieve this?
To count the distance, when creating a bullet store it's position somewhere. Then every step check:
b2Vec2 diff = bullet->GetPosition() - startPosition;
if (diff.Length() > MaxLen)
{
world->DestroyBody(bullet);
}
EDIT:
if you want to calculate the path length then store somewhere the previous position and the path length, that is initially 0:
b2Vec2 diff = bullet->GetPosition() - prevPosition;
pathLength += diff.Length();
if (pathLength > MaxLen())
{
//destroy bullet//world->DestroyBody(bullet);
}
It's quite simple: world->DestroyBody(body).
And, small advice. For the good practice and performance you should not create bullets over and over again. Reuse it! Just make them invisible and reposition them at a position of a source.