We want to use ObjectiveChipmunk for our iPhone Games but i have problems to got it to work.
I have played with the SimpleObjectiveChipmunk Example and replaced the custom button with an UIImageView. Since 2 days i am trying to use the touches events(with pivot joint) to move the UIImageView and then let it fall down again.
Two Problems
1.
When i change the position of the UIImageView's body to"body.pos = cpv(0.0f, 0.0f);" in FallingButton.h then the view is not falling to the lower left corner at start, it falls ~50 pixel to the right. The Button does.
2.
The big Problem, i can not get it to work that i move the UIImageView with the touches events and let it fall down again.
There are weired jumps of the view, there seems to be a problem with the coordinates which i cant figure out.
Thank you for you help
I made minor changes(in red):
FallingButton.h
Code: Select all
#import <Foundation/Foundation.h>
#import "ObjectiveChipmunk.h"
@interface FallingButton : NSObject <ChipmunkObject> {
//UIButton *button;
UIImageView *button;
ChipmunkBody *body;
NSSet *chipmunkObjects;
int touchShapes;
}
//@property (readonly) UIButton *button;
@property (readonly) UIImageView *button;
@property (readonly) NSSet *chipmunkObjects;
@property int touchedShapes;
- (void)updatePosition;
@end
Code: Select all
/*
// Set up the UIButton.
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Click Me!" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0, 0, SIZE, SIZE);
*/
UIImage *image = [[UIImage alloc] init];
UIImageView *imageView = [[UIImageView alloc] init];
image = [UIImage imageNamed:@"logo.png"];
imageView = [[UIImageView alloc] initWithImage: image];
imageView.userInteractionEnabled = YES;
imageView.bounds = CGRectMake(0, 0, SIZE, SIZE);
button = imageView;
Code: Select all
@interface ViewController : UIViewController <UIAccelerometerDelegate> {
CADisplayLink *displayLink;
ChipmunkSpace *space;
FallingButton *fallingButton;
cpVect mousePoint;
cpVect mousePoint_last;
ChipmunkBody *mouseBody;
ChipmunkConstraint *mouseJoint;
}
@end
Code: Select all
// Create and initialize the Chipmunk space.
// Chipmunk spaces are containers for simulating physics.
// You add a bunch of objects and joints to the space and the space runs the physics simulation on them all together.
space = [[ChipmunkSpace alloc] init];
[space setGravity:cpv(0, 982)];
[space setElasticIterations:10];
// This method adds four static line segment shapes to the space.
// Most 2D physics games end up putting all the gameplay in a box. This method just makes that easy.
// We'll tag these segment shapes with the borderType object. You'll see what this is for next.
[space addBounds:self.view.bounds thickness:10.0f elasticity:1.0f friction:1.0f layers:CP_ALL_LAYERS group:CP_NO_GROUP collisionType:borderType];
.
.
.
mouseBody = [[ChipmunkBody alloc] initStaticBody];
Code: Select all
// This method is called each frame to update the scene.
// It is called from the display link every time the screen wants to redraw itself.
- (void)update {
// Step (simulate) the space based on the time since the last update.
cpFloat dt = displayLink.duration*displayLink.frameInterval;
cpVect newPoint = cpvlerp(mousePoint_last, mousePoint, 0.25f);
mouseBody.pos = newPoint;
mouseBody.vel = cpvmult(cpvsub(newPoint, mousePoint_last), 1.0f/dt);
mousePoint_last = newPoint;
[space step:dt];
// Update the button.
// This sets the position and rotation of the button to match the rigid body.
[fallingButton updatePosition];
}
Code: Select all
static float hh = 639.0/2.0, hw = 479.0/2.0;
static cpVect
mouseToSpace(cpVect pos, CGSize size)
{
cpVect v = cpvsub(cpvmult(cpv(pos.y/size.height, pos.x/size.width), 2.0f), cpv(1.0f, 1.0f));
v = cpv(v.x*hw, v.y*hh);
// printf("%s\n", cpvstr(v));
return v;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// We only support single touches, so anyObject retrieves just that touch from touches
UITouch *touch = [[event touchesForView:[self view]] anyObject];
cpVect point = mouseToSpace([touch locationInView:[self view]], [[touch view] bounds].size);
// NSLog(@"down %s", cpvstr(point));
mousePoint = point;
NSLog(@"%s\n", cpvstr(point));
ChipmunkShape *shape = [space pointQueryFirst:point layers:GRABABLE_MASK_BIT group:nil];
if(shape){
ChipmunkBody *body = shape.body;
mousePoint_last = mousePoint;
mouseBody.pos = mousePoint;
mouseJoint = [space add: [ChipmunkPivotJoint pivotJointWithBodyA:mouseBody bodyB:body anchr1:cpvzero anchr2:[body world2local:point]]];
mouseJoint.maxForce = 50000.0f;
mouseJoint.biasCoef = 0.15f;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event touchesForView:[self view]] anyObject];
mousePoint = mouseToSpace([touch locationInView:[self view]], [[touch view] bounds].size);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[space remove:mouseJoint];
mouseJoint = nil;
}