Because of the positioning of my wheels in relation to the chassis, the suspensions travels at roughly 45 degrees.
I realize that because my suspension is at an angle, not only will the piston sprite have to slide in and out of the sleeve sprite, but also the entire shock absorber with have to pivot as the spring joint compresses. Guess I made more work for myself - but I'm not setting for perpendicular shocks
What would be the best way to do this?
Partial Truck.m:(haven't done anything with shock sprites yet)
Code: Select all
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprites.plist"];
chassis = [CCSprite spriteWithSpriteFrameName:@"st_chassis.png"];
wheelFront =[CCSprite spriteWithSpriteFrameName:@"mt_front_wheel.png"];
wheelRear =[CCSprite spriteWithSpriteFrameName:@"mt_rear_wheel.png"];
shockPiston =[CCSprite spriteWithSpriteFrameName:@"mt_shock_piston.png"];
shockSleve =[CCSprite spriteWithSpriteFrameName:@"mt_shock_piston.png"];
cpFloat cMass = 5.0f;
cpFloat cWidth = 172;
cpFloat cHeight = 72;
cpFloat radius = 45.0f;
cpFloat mass = 5.0f;
chassisBody = [ChipmunkCocosBody bodyWithMass:cMass andMoment:cpMomentForBox(cMass, cWidth, cHeight)];
chassisBody.pos = pos;
cpVect frontWheelPos = cpv( pos.x + cWidth/2, pos.y - radius*2);
cpVect rearWheelPos = cpv(pos.x - cWidth/2, pos.y - radius*2);
chassisShape = [ChipmunkPolyShape boxWithBody:chassisBody width:cWidth height:cHeight];
chassisShape.friction = 0.7;
chassisShape.group = self;
wheelFrontBody = [ChipmunkCocosBody bodyWithMass:mass andMoment:cpMomentForCircle(mass, 0, radius, cpvzero)];
wheelFrontBody.pos = frontWheelPos;
wheelFrontShape = [ChipmunkCircleShape circleWithBody:wheelFrontBody radius:radius offset:cpvzero];
wheelFrontShape.friction = 1.0;
wheelFrontShape.group = self;
wheelRearBody = [ChipmunkCocosBody bodyWithMass:mass andMoment:cpMomentForCircle(mass, 0, radius, cpvzero)];
wheelRearBody.pos = rearWheelPos;
wheelRearShape = [ChipmunkCircleShape circleWithBody:wheelRearBody radius:radius offset:cpvzero];
wheelRearShape.friction = 1.0;
wheelRearShape.group = self;
float frontJointStartX = 50;
float frontJointStartY = 16;
float rearJointStartX = 50;
float rearJointStartY = 10;
float jointEndY = 65;
float frontSpringRestLength = jointEndY - frontJointStartY+30;
float rearSpringRestLength = jointEndY - rearJointStartY+30;
int springStiffness = 270;
int damping = 3;
int jointOffset = 57; //pushes wheel beyond jointStartX, giving us an angled connection to the chassis
frontGroove = [ChipmunkGrooveJoint grooveJointWithBodyA:chassisBody
bodyB:wheelFrontBody
groove_a:cpv(frontJointStartX, -frontJointStartY)
groove_b:cpv(frontJointStartX+jointOffset, -jointEndY)
anchr2:cpvzero];
rearGroove = [ChipmunkGrooveJoint grooveJointWithBodyA:chassisBody
bodyB:wheelRearBody groove_a:cpv(-rearJointStartX, -rearJointStartY)
groove_b:cpv(-rearJointStartX-jointOffset, -jointEndY)
anchr2:cpvzero];
sf = [ChipmunkDampedSpring dampedSpringWithBodyA:chassisBody
bodyB:wheelFrontBody anchr1:cpv(frontJointStartX, -frontJointStartY)
anchr2:cpvzero
restLength:frontSpringRestLength
stiffness:springStiffness
damping:damping];
sr = [ChipmunkDampedSpring dampedSpringWithBodyA:chassisBody
bodyB:wheelRearBody
anchr1:cpv(-rearJointStartX, -rearJointStartY)
anchr2:cpvzero
restLength:rearSpringRestLength
stiffness:springStiffness
damping:damping];
chipmunkObjects = [[NSArray alloc] initWithObjects:
chassisBody,
chassisShape,
wheelFrontBody,
wheelFrontShape,
wheelRearBody,
wheelRearShape,
frontGroove,
rearGroove,
sf,
sr,
nil];
sprites = [[NSArray alloc] initWithObjects:chassis, wheelFront, wheelRear, nil];
chassisBody.syncedNodes = [NSArray arrayWithObject:chassis];
wheelFrontBody.syncedNodes = [NSArray arrayWithObject:wheelFront];
wheelRearBody.syncedNodes = [NSArray arrayWithObject:wheelRear];