Comments (5)
Well, this took me way too long to see 😂 (The error message really isn't helping in this case)
world.setGravity takes a Vec2
as argument
- world.setGravity(0,3);
+ world.setGravity(Vec2(0,3));
I would recommend to use Typescript (Vite actually supports it out of the box, but you have to run tsc
in a separate process if you want errors reported by a script (otherwise the editor would still show errors on opened files))
from planck.js.
No worries! Glad it works now 🙂
from planck.js.
Hey, thank you for reporting the issue! I wasn't able to reproduce the bug yet. Are you running something like the following code or something entirely different?
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser-arcade-physics.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/planck/dist/planck.js"></script>
</head>
<body>
<script>
let game;
window.onload = function() {
let gameConfig = {
type: Phaser.AUTO,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
parent: "thegame",
width: 600,
height: 600
},
scene: playGame
}
game = new Phaser.Game(gameConfig);
window.focus();
}
class playGame extends Phaser.Scene{
constructor(){
super("PlayGame");
}
create(){
// Box2D works with meters. We need to convert meters to pixels.
// let's say 30 pixels = 1 meter.
this.worldScale = 30;
// world gravity, as a Vec2 object. It's just a x, y vector
let gravity = planck.Vec2(0, 3);
// this is how we create a Box2D world
this.world = planck.World(gravity);
// createBox is a method I wrote to create a box, see how it works at line 55
this.createBox(game.config.width / 2, game.config.height - 20, game.config.width, 40, false);
// the rest of the script just creates a random box each 500ms, then restarts after 100 iterations
this.tick = 0;
this.time.addEvent({
delay: 500,
callbackScope: this,
callback: function(){
this.createBox(Phaser.Math.Between(100, game.config.width - 100), -100, Phaser.Math.Between(20, 80), Phaser.Math.Between(20, 80), true);
this.tick ++;
if(this.tick == 100){
this.scene.start("PlayGame");
}
},
loop: true
});
}
// here we go with some Box2D stuff
// arguments: x, y coordinates of the center, with and height of the box, in pixels
// we'll conver pixels to meters inside the method
createBox(posX, posY, width, height, isDynamic){
// this is how we create a generic Box2D body
let box = this.world.createBody();
if(isDynamic){
// Box2D bodies born as static bodies, but we can make them dynamic
box.setDynamic();
}
// a body can have one or more fixtures. This is how we create a box fixture inside a body
box.createFixture(planck.Box(width / 2 / this.worldScale, height / 2 / this.worldScale));
// now we place the body in the world
box.setPosition(planck.Vec2(posX / this.worldScale, posY / this.worldScale));
// time to set mass information
box.setMassData({
mass: 1,
center: planck.Vec2(),
// I have to say I do not know the meaning of this "I", but if you set it to zero, bodies won't rotate
I: 1
});
// now we create a graphics object representing the body
var color = new Phaser.Display.Color();
color.random();
color.brighten(50).saturate(100);
let userData = this.add.graphics();
userData.fillStyle(color.color, 1);
userData.fillRect(- width / 2, - height / 2, width, height);
// a body can have anything in its user data, normally it's used to store its sprite
box.setUserData(userData);
}
update(){
// advance the simulation by 1/20 seconds
this.world.step(1 / 30);
// crearForces method should be added at the end on each step
this.world.clearForces();
// iterate through all bodies
for (let b = this.world.getBodyList(); b; b = b.getNext()){
// get body position
let bodyPosition = b.getPosition();
// get body angle, in radians
let bodyAngle = b.getAngle();
// get body user data, the graphics object
let userData = b.getUserData();
// adjust graphic object position and rotation
userData.x = bodyPosition.x * this.worldScale;
userData.y = bodyPosition.y * this.worldScale;
userData.rotation = bodyAngle;
}
}
};
</script>
</body>
</html>
from planck.js.
hii @zOadT .. firstly Thank You for your response
Here is what's in my project:
- Clone official vite + phaser templete from here
- i installed dependencies required by this template using
npm install
- then installed Planck.js using
npm
- After all this Only file i edited was
scr/Scenes/Game.js
..Here it is. - And run the development server using
npm run dev
( firstly Main Menu Scene appears if you click on it then comes the the Game.js Scene)
I hope this will help you solving this issue
from planck.js.
Thanks a lot @zOadT and sorry for silly mistake 😅...
Now i realised the need for typescript...
from planck.js.
Related Issues (20)
- When will version 1 be released? HOT 5
- Feature Request: Implementation of b2Draw, SetDebugDraw and DebugDraw HOT 7
- Changing a circle collider's friction doesn't make it roll slower HOT 1
- Feature Request: Add the ESM button to jsDelivr
- How to change a collider's center? HOT 5
- Why does displacement cause multiple begin and end contact events? HOT 6
- Typo in the Wiki HOT 4
- setActive(true) throwing error in v1 beta 8
- ERROR changing a collider's mass cancels its linearVelocity HOT 5
- planck's npm package is quite large HOT 8
- World step error HOT 12
- possible testOverlap bug HOT 2
- Raycasts phasing through walls HOT 6
- clarification on the determinism limitation from docs HOT 1
- pointers on how to network planck HOT 8
- inconsistent Vec2 construction throughout examples
- DistanceJoint `getReactionForce` function not working HOT 4
- Script tag: Uncaught TypeError: t is null HOT 2
- Cannot read properties of undefined (reading "x") HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from planck.js.