objects with unintended movement

Discuss new features and future development.
Post Reply
crm357
Posts: 5
Joined: Sat Feb 04, 2012 3:41 pm
Contact:

objects with unintended movement

Post by crm357 »

i realize this is a bit long but i am at the point of needing detailed guidance to explain what i'm seeing.

i have two UIImageView objects containing images of what will become a fairly complex animated character. i currently have it's body and an arm in place and am looking to chipmunk to manage movement between the two, however, before i even specify any movement to the chipmunk objects, when these two objects first appear on the screen they move slightly (relative to each other). why?

commenting out the affineTransform assignment in the chipmunk object's updatePosition method the two images hit the screen and stay put. when i uncomment the assignment and rerun the app when the images hit the screen the character's body moves slightly (~80 pixels) to the left as the character's arm moves what appears to be the same amount to the right.

in the viewDidLoad segment below, as is, the character's body does not move (even with the affineTransform assignment uncommented). uncomment '#define SECONDPART 1' and rerun and the body as well as the arm moves.

what is causing this (unintended) movement and how can i stop it?

the UIViewController's viewDidLoad where chipmunk entites are created:

Code: Select all

- (void)viewDidLoad
{
	CGRect r;

    	[super viewDidLoad];
	. . .

	characterSpace = [[ChipmunkSpace alloc] init];

	UIImage *im = [UIImage imageNamed:@"characterBody.png"];
	self.characterBody = [[UIImageView alloc] initWithImage:im];
	characterBody.frame = CGRectMake(100.0, 100.0, im.size.width, im.size.height);
	[self.view addSubview:characterBody];
	self.CPcharacterBody = [[characterPart alloc] initWithImage:characterBody];
	[characterSpace add:CPcharacterBody];
//#define SECONDPART 1
#ifdef SECONDPART
	im = [UIImage imageNamed:@"characterArm.png"];
	self.characterArm = [[UIImageView alloc] initWithImage:im];
	characterArm.frame = CGRectMake(300.0, 150.0, im.size.width, im.size.height);
	[self.view addSubview:characterArm];
	self.CPcharacterArm = [[characterPart alloc] initWithImage:characterArm];
	[characterSpace add:CPcharacterArm];
#endif
	. . .
}
the ChipmunkObject used for the character parts:

Code: Select all

@interface characterPart : NSObject <ChipmunkObject>
{
	UIImageView *image;
	ChipmunkBody *body;
	NSArray *chipmunkObjects;
}

@property (strong,nonatomic) UIImageView *image;
@property (strong,nonatomic) ChipmunkBody *body;
@property (strong,nonatomic) NSArray *chipmunkObjects;

-(id)initWithImage:(UIImageView *)image;
-(void)updatePosition;

@end
and

Code: Select all

@implementation characterPart

@synthesize image;
@synthesize body;
@synthesize chipmunkObjects;

-(id)initWithImage:(UIImageView *)partImage
{
	if(self = [super init]) {
		cpFloat mass = 1.0;
		cpFloat moment = cpMomentForBox(mass, partImage.bounds.size.width, partImage.bounds.size.height);
		body = [[ChipmunkBody alloc] initWithMass:mass andMoment:moment];
		body.pos = cpv(partImage.frame.origin.x,partImage.frame.origin.y);
		ChipmunkShape *shape = [ChipmunkPolyShape boxWithBody:body width:partImage.bounds.size.width height:partImage.bounds.size.height];
		chipmunkObjects = [[NSArray alloc] initWithObjects:body,shape,nil];
		self.image = partImage;
	}
	return self;
}

-(void)updatePosition
{
	image.transform = body.affineTransform;
}

@end
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests