Moving a cocos2d-iphone+box2d boy in one direction - iphone

Please can someone advice me on this.
I have a cocos2d-iphone +box2d body I wish to move only in one direction(movement only from left to right of the iphone screen) using only the accelerometer controls and parallax scrolling. Please how do I do this as at the moment the body moves both to the left and right once the device is tilted. I do not want to use forces or impulse since I will NOT be enabling touch controls.
This is my accelerometer code :
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration
*)acceleration {
float32 accelerationFraction = acceleration.y * 6;
if (accelerationFraction < -1) {
accelerationFraction = -1;
} else if (accelerationFraction > 1) {
accelerationFraction = 1;
b2Vec2 gravity(-acceleration.y *10, acceleration.x *10);
world->SetGravity( gravity );
}
Thanks in advance for your help .

You should use mouseJoints. Look at this tutorial. There he explains exactly what you are trying to accomplish.

Related

apply force to a body in touchesMoved direction in box2d iphone

I am new to box2d. I have a problem i.e
when i touch the body and touchmoved(In touchmoved method i don't want move the body)upto some place and touch released.
in touchend i apply force like this...
b2Vec2 force = b2Vec2(0.0,15.0);
ballBody->SetLinearVelocity(force);
and find out the angle b/w touchbegin point to touchend point in this way
float angleRadians = atan2(touchBegin.x - touchEnd.x, touchBegin.y - touchEnd.y);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
cocosAngle1 = -1 * angleDegrees;
in that time,how to apply force to the body with cocosAngle1(touchend direction). Like paper in "Paper Toss" game.
how to apply force in touchmoved direction of body?...
please explain to me...

Moving a sprite with touches began, casting and parameters in obj - c?

I've been reading a book on the cocos2d framework for ios5 for a few days and have developed a small game that the book walks you through. To control the sprite in this game you use the accelerometer:
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float deceleration = 0.4f;
float sensitivity = 6.0f;
float maxVelocity = 100;
// adjust velocity based on current accelerometer acceleration
playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
// we must limit the maximum velocity of the player sprite, in both directions (positive & negative values)
if (playerVelocity.x > maxVelocity)
{
playerVelocity.x = maxVelocity;
}
else if (playerVelocity.x < -maxVelocity)
{
playerVelocity.x = -maxVelocity;
}
// Alternatively, the above if/else if block can be rewritten using fminf and fmaxf more neatly like so:
// playerVelocity.x = fmaxf(fminf(playerVelocity.x, maxVelocity), -maxVelocity);
}
Now I'm wondering if I can change this code to allow the sprite to still accelerate/decelerate along the x axis, but to use touch input rather than the accelerometer, and to go faster the longer the touch is held down for? So a touch to the right would move the sprite to that spot slowly, if the touch is released, it stops moving to that spot. The longer a touch is held down, the faster the sprite moves.
is there anything in the framework to allow me to implement a rotation mechanism that allows my sprite to rotate to the position that the touch was in, so it looks like its facing the point thats been touched?
Well, afaik theres no method that will determine the angle to the touch and then rotate the sprite accordingly, but if you have the x and y coordinates of the sprite and the touch you can calculate it yourself fairly easily.
CGPoint spriteCenter; // this should represent the center position of the sprite
CGPoint touchPoint; //location of touch
float distanceX = touchPoint.x - spriteCenter.x;
float distanceY = touchPoint.y - spriteCenter.y;
float angle = atan2f(distanceY,distanceX); // returns angle in radians
// do whatever you need to with the angle
Once you have the angle you can set the rotation of the sprite.

Detecting that the device moved

I am creating a motion detection alarm for the iPhone. This alarm will be set by pressing an 'Activate' button, and after a small countdown, will then take readings about whether it is moving. I have tried using the accelerometer to detect movement, but if the threshold becomes too low it activates constantly, whether the device is moving or not. If I set the threshold higher it does not detect movement, and will only detect specific speeds of motion.
Does anyone have any solutions as to how to do this correctly, not by an amount of force, but whether the phone has actually moved.
I have tried to find online examples of how to implement it but only came up with solutions for augmented reality and OpenGL etc. All I need is a simple detection of motion.
Thank you for any help!
EDIT:
I managed to get it working using some samples from the Apple documentation.
I've managed to achieve what I want using this accelerometer didAccelerate method:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Use a basic low-pass filter to keep only the gravity component of each axis.
accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
float accelerationThreshold = 1.01; // or whatever is appropriate - play around with different values
if (fabs(accelX) > accelerationThreshold || fabs(accelY) > accelerationThreshold || fabs(accelZ) > accelerationThreshold) {
[self.soundPlayer play];
accelX = accelY = accelZ = 0;
accelerometerActive = NO;
accelerometer.delegate = nil;
accelerometer = nil;
}
}
One way to do this could be to calculate a vector for gravity. If the phone isn't moving then the vector will remain fairly constant. Should the phone be picked up it is likely that the orientation of the phone will change, thus the gravity vector will also change (in relation to the orientation of the accelerometer).
To detect movement simply calculate the angle between the original vector set on 'Activate' and the current vector. Googling angles between 3d vectors will help you out with the mathmatics behind this.
Theoretically someone could pick the phone up and maintain the exact orientation but this is incredibly unlikely.
Hope this helps and good luck.

Move object into screen without gravity into box2d in iPhone

i use following code to move my Box2D object into screen, but because gravity of my world or something else i dont know why my objects is forced to move down, i am new to box2d.
i want to move my object in entire world without gravity.
-(void) tick:(NSTimer *)timer {
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(1.0f/60.0f, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL)
{
UIView *oneView = (UIView *)b->GetUserData();
CGPoint newCenter = CGPointMake(b->GetPosition().x * PTM_RATIO,self.view.bounds.size.height - b->GetPosition().y * PTM_RATIO);
oneView.center = newCenter;
CGAffineTransform transform = CGAffineTransformMakeRotation(- b->GetAngle());
oneView.transform = transform;
}
}
}
my accelerometer code is as follow.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
b2Vec2 gravity;
gravity.Set( acceleration.x * 1.81, acceleration.y * 1.81 );
world->SetGravity(gravity);
}
pleas if any one work around.
Thanks.
As i understand you want to move your object setting it's position. It's a bad idea because it will provide non-physical behavior of the bodies colliding with your object. That's because if you will only change the position of your body it's velocity for physics engine will be still zero and collision will be processed according to zero speed of your object.
A better solution is to use b2_kinematicBody type for your object. Then you will be able to control it's motion specifying it's velocity vector and physics will behave as expected. Also the gravity (and no other forces) will not be applied to your object because of it's type.
EDIT
//creation
b2BodyDef bDef;
bDef.type = b2_kinematicBody;
bDef.position.Set(5, 6);
b2Body *body = physWorld->CreateBody(&bDef);
//control
body->SetLinearVelocity(b2Vec2(3, 4));

How to detect when iPhone is moving in X direction?

How do I detect that the iPhone is moving in a particular direction (X-axis)? Can you show some sample code?
You can do it with help of UIAccelerometer and check the movement fo phone in x-axis by:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)aceler {
if (sqrt(aceler.x * aceler.x) > 1.5f)
{
//write your code here
}
}
Hope this helps.
Thanks,
Madhup