wichiwichi Posted January 30, 2014 Share Posted January 30, 2014 Hi all, so I'm trying to detect whether a car is off or on the track. I tried previously with overlap but had de problem that even when almost all of the car is out, track and car still overlap. To correct this I was told to create a group of sprites, the first being the picture of the car, and four other which would be invisible and would detect whether that part of the car was off track. So I have two issues: first, a sanity check. Is this the way it is supposed to be? Isn't it possible to apply all the body.functions to carGroup instead of having to apply them to 5 sprites independently? carGroup = game.add.group(); car = carGroup.create(200, 200, 'car'); car.anchor.setTo(0.5, 0.5); car.scale.setTo(0.5, 0.5); // This will force it to decelerate and limit its speed car.body.drag.setTo(200, 200); car.body.maxVelocity.setTo(400, 400); car.body.collideWorldBounds = false; car.body.setSize(200, 200, 0, 0); //car.body.acceleration.setTo(9,9); wheel1 = carGroup.create(205,235); wheel1.anchor.setTo(0.5, 0.5); wheel1.scale.setTo(0.5, 0.5); // This will force it to decelerate and limit its speed wheel1.body.drag.setTo(200, 200); wheel1.body.maxVelocity.setTo(400, 400); wheel1.body.collideWorldBounds = false; wheel1.body.setSize(80, 20, 0, 0);Same issue with update function: if ((cursors.left.isDown || touchTurn == -1) && currentSpeed > 0) { car.angle -= currentSpeed/170; wheel1.angle = car.angle; //etc. }Second issue, possibly because of my previous mistakes: Here you can see the "wheel" in a more or less ok position When the group turns, as they do so independently, they don't turn keeping the relative position within the elements, so the "wheel" falls in the middle of the car, not in the right side What shoud I do? Thank you for all the answers you've given me before Cheers Link to comment Share on other sites More sharing options...
rich Posted January 31, 2014 Share Posted January 31, 2014 Use sprite.pivot to position your wheels and not anchor, the pivot will be relative to the parent (it's a pixel valued point object), this should help get around the issues with the wheel alignment. Heppell08 1 Link to comment Share on other sites More sharing options...
wichiwichi Posted February 1, 2014 Author Share Posted February 1, 2014 Thanks, so I get the concept, but now wheel and chassis are going in opposite directions. I assume they are pivoting from a point, but wheel is not attached to chassis car = game.add.group(); chassis = car.create(200, 200, 'chassis'); chassis.anchor.setTo(0.5, 0.5); chassis.scale.setTo(0.5, 0.5); chassis.body.collideWorldBounds = false; chassis.body.setSize(200, 200, 0, 0); wheel1 = car.create(205,235); wheel1.pivot = chassis; wheel1.body.collideWorldBounds = false; wheel1.body.setSize(80, 20, 0, 0); As for velocities in update function, all must be applied to car now, right? Thank you so much Link to comment Share on other sites More sharing options...
Recommended Posts