Ball Randomly Slowing Down - cocos2d-iphone

Currently, I have a program that has a ball, bouncing back and forth. In order to find out how fast it's bouncing, I made this:
b2Vec2 velocity = blueCircleBody->GetLinearVelocity();
float constantSpeed = velocity.Length();
NSLog(#"%.f", constantSpeed);
This is inside a tick: function. Every second, I want to see how fast the ball is moving. As I notice in the Output window, the ball seems to slow down after few minutes.
2012-12-16 11:29:10.458 Bubble Fill V1[31623:c07] 30
2012-12-16 11:29:11.474 Bubble Fill V1[31623:c07] 30
2012-12-16 11:29:12.475 Bubble Fill V1[31623:c07] 30
2012-12-16 11:29:13.492 Bubble Fill V1[31623:c07] 24
2012-12-16 11:29:14.525 Bubble Fill V1[31623:c07] 24
Is there a reason why the ball randomly went from 30 to 24?
Additionally, I have another problem. If I restart the program, with all the same data, the ball is going at a different speed for unknown reason. This means that the impulse/force I use doesn't dictate how fast it's going.
Here's some more information on the ball:
blueCircle = [CCSprite spriteWithFile:#"blueCircle.png"];
blueCircle.position = ccp(winSize.width/4, winSize.height/4);
blueCircle.tag = 2;
[self addChild:blueCircle];
blueCircleBodyDef.type = b2_dynamicBody;
//Find a way to make the BoundingBox/GroundBox to not be affected by the Force of the Walls
blueCircleBodyDef.position.Set(winSize.width/4/PTM_RATIO, winSize.height/4/PTM_RATIO);
//blueCircleBodyDef.position.Set(blueCircle.position.x/PTM_RATIO, blueCircle.position.y/PTM_RATIO);
blueCircleBodyDef.userData = blueCircle;
blueCircleBody = world->CreateBody(&blueCircleBodyDef);
float blueCircleRadius = blueCircle.contentSize.width/2 * blueCircle.scale;
blueCircleShape.m_radius = blueCircleRadius/PTM_RATIO;
blueCircleFixtureDef.shape = &blueCircleShape;
blueCircleFixtureDef.density = 1.0f;
blueCircleFixtureDef.friction = 0.0f;
blueCircleFixtureDef.restitution = 1.0f;
blueCircleBody->CreateFixture(&blueCircleFixtureDef);
force.Set(0, 140);
//blueCircleBody ->ApplyLinearImpulse(force, blueCircleBodyDef.position);
//blueCircleBody->SetLinearVelocity(force);
blueCircleBody->SetGravityScale(0);
blueCircleBody->SetLinearVelocity(force);

Related

How to make an Smooth Movement in C++, SFML

I have a problem doing an smooth movement in c++ w/ sfml.
Someone can make an algorithm to smooth move a shape / sprite by 15 px (the sprite / shape is 15 px) by pressing a button? Thanks.
Because many other programmers might want to know that, here it is, but next time ask a more precise question
Here's my way to make a smooth movement:
Take your initial movement, lets say 25, 15 per second
Make a loop like this:
void GameEngine::GameLoop()
{
sf::Clock timer;
sf::Time tickRate;
sf::Vector2f movement(25.0,15.0); // Your movement vector
while(/* wahtever */){
tickRate = timer.restart();
yourShape.move(movement * ((float)tickRate.asMilliseconds() / 1000)); //Move your shape depending on the time elapsed between two frame
yourWindow.clear();
yourWindow.draw(yourShape);
yourWindow.display();
}
}

Fixed Timestep at 2500 FPS?

I am using SFML making a 2D platformer. I read so many timestep articles but they don't work well for me. I am implementing it like 2500 FPS timestep, on my desktop pc it's amazingly smooth, on my laptop it's getting 300 FPS(I check with Fraps), it's not that smooth at laptop but still playable.
Here are the code snippets:
sf::Clock clock;
const sf::Time TimePerFrame = sf::seconds(1.f/2500.f);
sf::Time TimeSinceLastUpdate = sf::Time::Zero;
sf::Time elapsedTime;
These are variables and here is the game loop,
while(!quit){
elapsedTime = clock.restart();
TimeSinceLastUpdate += elapsedTime;
while (TimeSinceLastUpdate > TimePerFrame){
TimeSinceLastUpdate -= TimePerFrame;
Player::instance()->handleAll();
}
Player::instance()->render();
}
In the Player.h, I've got movement constants,
const float GRAVITY = 0.35 /2500.0f; // Uses += every frame
const float JUMP_SPEED = -400.0f/2500.0f; //SPACE -> movementSpeed.y = JUMP_SPEED;
//When character is touching to ground
const float LAND_ACCEL = 0.075 /2500.0f; // These are using +=
const float LAND_DECEL = 1.5 /2500.0f;
const float LAND_FRICTION = 0.5 /2500.0f;
const float LAND_STARTING_SPEED = 0.075; // This uses =, instead of +=
In the handleAll function of Player class, there is
cImage.move(movementSpeed);
checkCollision();
And lastly, checkCollision function, simply checks if character's master bounding box intersects the object's rectangle from each side, sets the speed x or y to 0, then fixes the overlapping by setting character position to the edge.
//Collision
if(masterBB().intersects(objectsIntersecting[i]->GetAABB())){
//HORIZONTAL
if(leftBB().intersects(objectsIntersecting[i]->GetAABB())){
if(movementSpeed.x < 0)
movementSpeed.x = 0;
cImage.setPosition(objectsIntersecting[i]->GetAABB().left + objectsIntersecting[i]->GetAABB().width + leftBB().width , cImage.getPosition().y);
}
else if(rightBB().intersects(objectsIntersecting[i]->GetAABB())){
if(movementSpeed.x > 0)
movementSpeed.x = 0;
cImage.setPosition(objectsIntersecting[i]->GetAABB().left - rightBB().width , cImage.getPosition().y);
}
//VERTICAL
if(movementSpeed.y < 0 && topBB().intersects(objectsIntersecting[i]->GetAABB())){
movementSpeed.y = 0;
cImage.setPosition(cImage.getPosition().x , objectsIntersecting[i]->GetAABB().top + objectsIntersecting[i]->GetAABB().height + masterBB().height/2);
}
if(movementSpeed.y > 0 && bottomBB().intersects(objectsIntersecting[i]->GetAABB())){
movementSpeed.y = 0;
cImage.setPosition(cImage.getPosition().x , objectsIntersecting[i]->GetAABB().top - masterBB().height/2);
//and some state updates
}
}
I tried to use 60 FPS Timestep like million times but all speed variables become so slow, I can't simply do like *2500.0f / 60.0f to all constants, It doesn't feel same. If I get close constants, It feels "ok" but then when the collision happens, character's position is getting setted all the time and it flys out of the map because of the big lap on the object caused by high speed constants applied every frame I guess...
I need to add, Normally, the book I took the timestep code uses
cImage.move(movementSpeed*TimePerFrame.asSeconds());
but as you saw, I just put /2500.0f to every constant and I don't use it.
So, is 1/2500 seconds per frame good? If not, how can I change all of these to 1/60.0f?
You're doing it wrong.
Your monitor most likely has a refresh rate of 60 Hz (= 60 FPS), thus trying to render an image at 2500 FPS is a huge waste of resources. If the only reason for choosing 2500 FPS is that your movement doesn't work the same, haven't you ever thought about, that the problem then might be with the movement code?
At best you'd implement a fixed timestep (famous article), that way your physics can run at whatever rate you want (2500 "FPS" would still be crazy, so don't do it) and is independent from your rendering rate. So even if you get some varying FPS, it won't influence your physics.

SFML - Move object towards coord

so I am working on an AI system with C++ and SFML in which I want the enemies to follow the player. Im going to create it so the enemies are moving towards the point at which the player was 30 frames ago (so it will constantly updating). But my question is simple, what is the math behind getting the enemy to move to a specific point? Like let's say the player is at (230, 400) and the enemy is at (100, 200) how do I get the enemy (using the .move() function with a speed of 3) to move to that point? Any help would be fantastic!
---UPDATED BELOW WITH CURRENT CODE FOR HANDLING MOVEMENT-----
float angle = atan2(v2iEnemyPos[1].y - rsPlayer.getPosition().y, v2iEnemyPos[1].x - rsPlayer.getPosition().x);
angle =angle * 180 / (atan(1) * 4);
sf::Vector2f newpos((cos(angle))*2, (sin(angle))*2);
std::cout << newpos.x << " " << newpos.y << std::endl;
rsEnemy[1].move(newpos.x, newpos.y);
rwWinOne.draw(rsPlayer);
rwWinOne.display();
rwWinOne.clear();
The direction to move your enemy into is simply the difference between the player position and the enemy position. However, you want the enemies to move with constant speed, so you need to normalize the result. This will give you the direction as a vector of length 1.
Vector2u direction = normalize(player.getPosition() - enemy.getPosition());
You can now multiply this direction by the speed constant of that enemy's type. The result is a vector with it's length depending on the speed factor instead of the distance to the player. Just use the result to move your enemy.
enemy.move(speed * direction);
However, you perform that once a frame and framerates can vary between machines and configurations. Therefore, you should add a the elapsed time since the last move call, which might be your frame time, as factor to the equation. This way, if a frame takes a longer than usual, the enemy will be moved further to comprehend this, and vice versa.
// Initialize a timer
sf::Clock clock;
// Get elapsed time at the beginning of a frame
// I multiply this by target framerate to get a value around one
float delta = clock.restart().asSeconds() * 60;
// Move all objects based on elapsed time
Vector2u direction = normalize(player.getPosition() - enemy.getPosition());
enemy.move(delta * speed * direction);
By the way, basic knowledge of linear algebra is needed very often in game development, so you might pay out to attend an online course.
You should consider the elapsed time (since last frame) - dt - in your calculation.
The enemy has a given speed: enemy_velocity.
I won't use the move() function, but the SetPosition().
You have to calculate the new position of the enemy:
enemy.x += dt * enemy_velocity * cos(angle);
enemy.y += dt * enemy_velocity * sin(angle);
And affect it with SetPosition().

Cocos2D+Box2D: Stack bodies that doesn´t fall?

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:

CCMoveTo: speed of move

I have some problem with CCMoveTo:
id actionMove = [CCMoveTo actionWithDuration:3 position:ccp(pointBoard[x][y].x,pointBoard[x][y].y)];
for example my sprite start move from ccp(20,460) and move to ccp(20,0) it's ok. But when sprite need to move to ccp(20,200) than movement speed become slower.
I need to move sprite with the same speed. How can i do it?
Thanks.
You need to calculate the 'distance' between your [start] and [end] points and then you can calculate the 'duration' so that your sprite moves with constant speed. Something like,
float speed = 1; // here you define the speed that you want to use.
CGPoint start = sprite.position; // here you will get the current position of your sprite.
CGPoint end = ccp(pointBoard[x][y].x,pointBoard[x][y].y);
float distance = ccpDistance(start, end); // now you have the distance
float duration = distance/speed; // here you find the duration required to cover the distance at constant speed
Now you can call the CCMoveTo function and provide above calculated duration to make your sprite move at same speed.
Hope it helps..!!
To keep your movement speed constant for all distances, define a speed you need to move the sprite with and use the speed-time-distance formula you once learned as a child in your physics class to find an unknown from the three.
float speed = 50.0f;
id duration = ccpDistance(sprite.position, pointBoard[x][y]) / speed;
id moveAction = [CCMoveTo actionWithDuration:duration position:pointBoard[x][y]];
Here the speed of the sprite varies based on the distance.if the distance from ccp(20,460) to ccp(20,0) is same as ccp(20,0) to ccp(20,200).Speed remains same.But if the distance varies the speed varies accordingly(if the duration is same).
You can reduce the time if u want more speed.
Just use simple maths (time = distance/ speed) to calculate the time required for moveAction.
float speed = 13.0;
CGPoint startPoint = ccp(20,300);
CGPoint endPoint = ccp(20,100);
float time = ccpDistance(startPoint, endPoint) / speed;
id moveAction = [CCMoveTo actionWithDuration:time position:endPoint];
You Can spped mantain of speed variable and Position mainatain position:CGPointMake action.
float speed = 3.67;
CCMoveTo *moveuserleft;
CCMoveTo *moveuserleft2;
moveuserleft = [CCMoveTo actionWithDuration:speed position:CGPointMake(235*scaleX,200*scaleY)];
moveuserleft2 = [CCMoveTo actionWithDuration:speed position:CGPointMake(360*scaleX,200*scaleY)];
CCSequence *scaleSeqleft = [CCSequence actions:moveuserleft,moveuserleft2, nil];
[user runAction:scaleSeqleft];