-
efaresLikes 0Problem Description
Hello,
I'm using Cocos2d-x v3.7 (JavaScript) with Chipmunk Physics.
To add a physics sprite I am doing this:this.carSprite = new cc.PhysicsSprite.create("#car.png"); // OLD METHOD var carSize = this.carSprite.getContentSize(); // Create and add the car's physics body var carBody = new cp.Body(1, cp.momentForBox(1, carSize.width, carSize.height)); this.space.addBody(carBody); // Create and add the car's physics shape var carShape = new cp.BoxShape(carBody, carSize.width, carSize.height); this.space.addShape(carShape); this.carSprite.setBody(carBody); playSceneSpriteSheet.addChild(this.carSprite);
I'm not sure if this is the best way to add a physics sprite but it works fine and produces this:
Now I want to resize the physics sprite based on dimensions of my map so I do this:
this.carSprite = new cc.PhysicsSprite.create("#car.png"); // NEW METHOD var carSize = this.carSprite.getContentSize(); var spaceForCar = [math to calculate allotted space]; var scaleCarSize = spaceForCar / carSize.height; this.carSprite.setScale(scaleCarSize); this.carSprite.setContentSize(cc.size(carSize.width * scaleCarSize, carSize.height * scaleCarSize)); carSize = this.carSprite.getContentSize(); // new contentSize // Create and add the car's physics body var carBody = new cp.Body(1, cp.momentForBox(1, carSize.width, carSize.height)); this.space.addBody(carBody); // Create and add the car's physics shape var carShape = new cp.BoxShape(carBody, carSize.width, carSize.height); this.space.addShape(carShape); this.carSprite.setBody(carBody); playSceneSpriteSheet.addChild(this.carSprite);
And this is produced:
Any idea how to align and centre the physics shape to the physics body after modifying the sprite size?
-
Sonar Systems adminLikes 0
Try setting the anchor point.
This reply has been verified.
-
efaresLikes 0
That doesn’t work.
When I try to set the anchor point for either carBody or carShape it gives me an error saying carBody.setAnchorPoint is not a function and carShape.setAnchorPoint is not a function.
-
Sonar Systems adminLikes 0
Try creating a custom physics body using this video
This reply has been verified.
-
efaresLikes 0
I don’t suppose there’s a JavaScript version of this video is there?
-
Sonar Systems adminLikes 0
Not ATM sorry.
-
efaresLikes 0
I’m looking at this page: http://cocos.sonarlearning.co.uk/docs/nodes and it says “Warning: Physics body doesn't support this [scaling]”
Could this be the reason of my physics issue?
-
Sonar Systems adminLikes 0
It would appear, so good spot. Best bet is to create a custom physics body using this tutorial http://www.sonarlearning.co.uk/coursepage.php?topic=game&course=cocos2d-x-v3-cplusplus-physics&videoindex=441#441
This reply has been verified.
-
efaresLikes 0
That is a good tutorial but I have a JS project so how do I go about handling the MyBodyParser.cpp and .h file? As well as the required json folder? I don’t see it in the external folder of my JavaScript Cocos project.
Thanks for the quick replies!
-
Sonar Systems adminLikes 0
Check this out https://chipmunk-physics.net/forum/viewtopic.php?f=1&t=4501
This reply has been verified.
-
efaresLikes 0
Awesome, thanks but I just discovered something.
When I comment out this line:
// this.carSprite.setContentSize(cc.size(carSize.width * scaleCarSize, carSize.height * scaleCarSize));
So I don’t modify the car’s contentSize and then modify this line:
var carShape = new cp.BoxShape(carBody, carSize.width - scaleDifferenceOfWidth, carSize.height - scaleDifferenceOfHeight);
I can set the shape size while still keeping the anchors centered. It works perfectly fine this way :)
This reply has been verified.
-
Sonar Systems adminLikes 0
Fantastic.
I am really looking forward to your game, feel free to showcase it at http://www.sonarlearning.co.uk/questions.php?question-category=question-category-showcase and we will post about it via our social media and blog.
This reply has been verified.
-
efaresLikes 0
Awesome.
I’ll definitly keep that in mind. But might be a while, we’re only a 2-person team. 1 developer and 1 designer.
Coming soon though haha.This reply has been verified.
-
Sonar Systems adminLikes 1
Small steps :D
-
Sonar Systems adminLikes 1
Big things have small beginnings
Login to reply