Jump to content

physics library


Harryk89
 Share

Recommended Posts

DONT USE PIXIJS WITH PHYSICS LIBRARIES. We tell that every time to every person who ask. But you guys are stubborn.

p2, matter, box2d, people use everything even when we forbid them to do so.

Whatever you use, those two libs can help with editing shapes for your physcis engine:

https://github.com/eXponenta/pixi-poly - integration with physics-editor

https://github.com/eXponenta/pixi-tiled - integration with tiled (shapes, not tilemap!)

Link to comment
Share on other sites

not my intention to made publicity, but in this book, it show very easy code and basic ways to make very fast math physic for game with pixijs.
I recommend it for vanilla dev.
i never succes to work fine with p2 in pixijs, it not the same logic.
https://www.amazon.fr/Learn-Pixi-js-Create-Interactive-Graphics/dp/1484210956


0936kP5J_o.png

Full chapter on physics in pixi for people not good in advance math.

Link to comment
Share on other sites

6 hours ago, ivan.popelyshev said:

DONT USE PIXIJS WITH PHYSICS LIBRARIES. We tell that every time to every person who ask. But you guys are stubborn.

p2, matter, box2d, people use everything even when we forbid them to do so.

Whatever you use, those two libs can help with editing shapes for your physcis engine:

https://github.com/eXponenta/pixi-poly - integration with physics-editor

https://github.com/eXponenta/pixi-tiled - integration with tiled (shapes, not tilemap!)

I'm curious, why is it recommended not to use physics libs with pixijs?. Is the thought that devs should roll their own?

Or maybe I'm a tad daft and you were just joking?

Link to comment
Share on other sites

I'm curious, why is it recommended not to use physics libs with pixijs?. Is the thought that devs should roll their own?

> Or maybe I'm a tad daft and you were just joking?

I just dont know how to answer the question. To answer it - i have to code your game and just give it to you in a package :) 
 
I rewrote this post like 20 times and ate through 30 minutes at 3:30 AM-4:00 AM when I supposed to sleep. I dont know how to answer that.
 
I mean, I have to write a book to answer your question.
Link to comment
Share on other sites

Using physics libraries is ok in my opinion. People just need to know that there's no pre-existing integration, so you have to basically do one of these:

- Integrate physics library to pixi directly (I wouldn't recommend, that would break updates etc.)
- Run the physics and rendering separately and have some kind of synchronization between the two. This is the method I have used succesfully with matterjs.
- Build a plugin for physics syncing to pixi.

The second method I basically had all physically interacting game objects paired with a physics body and on each renderloop the transforms for those objects would be synced with physics bodies.

Link to comment
Share on other sites

I already have a working code base with Box2d and there doesn't seem to be any issues ... so just double checking if I was missing something.

OK, thought about that while i was sleeping. Finally got an answer:  its very good topic for book. As a result, we have books for 40$ and no open tutorials on physics integration :)

I had something with box2d+liquidfun but now i dont remember where is it. 

Here's the example of what can happen when you do integration with p2, with very helpful comments:

import { BodyOptions, Body, Box, Shape } from "p2";

import { PIXEL_TO_METR, METR_TO_PIXEL } from './constants';

export class PixiBody extends Body {
	display: PIXI.DisplayObject;
	_lastTransform: number;
	constructor(options: PixiBodyOptions | undefined, display: PIXI.DisplayObject) {
		if (!display) throw Error("Display object can't be null");

		options = options || {};
		options.boundsToShape = options.boundsToShape == undefined ? true : options.boundsToShape;
		const pos = display.position;
		const force = options.force ? options.force : { x: 0, y: 0 };
		const vel = options.velocity ? options.velocity : { x: 0, y: 0 };
		const opts: BodyOptions = {
			...(options as any), //because PixiBodyOptions have some other definitions
			...{
				position: [
                    -pos.x * PIXEL_TO_METR,
                    - pos.y * PIXEL_TO_METR
                ],
				velocity: [vel.x, vel.y],
				force: [force.x, force.y]
			}
		};

		super(opts);

		this.angle = display.rotation;
		this.display = display;

		if(options.shape instanceof Shape) {
			this.addShape(options.shape)
		}
		else if (options.boundsToShape) {
			const bounds = display.getLocalBounds();
			const box = new Box({ 
                width: Math.abs(bounds.width * PIXEL_TO_METR * display.scale.x),
				height: Math.abs(bounds.height * PIXEL_TO_METR * display.scale.y)
			 });
            
                box.material = options.material;

			//todo fixme, pass valid offset;
			//FIXME
			//FIIIIIIXXXXXMMMMMEEEEE

			this.addShape(box);
		}

		this._lastTransform = (this.display.transform as any)._localID;
	}

	update() {
        
        if (this._lastTransform !== (this.display.transform as any)._localID) {
        
            this.position[0] = -this.display.x * PIXEL_TO_METR;
			this.position[1] = -this.display.y * PIXEL_TO_METR;
			this.angle = this.display.rotation;
        
        } else {
			if (this.sleepState != Body.AWAKE) return;

			this.display.position.set(
                    -this.position[0] * METR_TO_PIXEL,
                    -this.position[1] * METR_TO_PIXEL);
			this.display.rotation = this.angle;
        }
        
		this._lastTransform = (this.display.transform as any)._localID;
    }
    
    destroy() {
        //???
        this.display = undefined;
    }
}

 

Link to comment
Share on other sites

6 hours ago, Exca said:

- Run the physics and rendering separately and have some kind of synchronization between the two. This is the method I have used succesfully with matterjs.

That's the technique more or less I'm using.

 

5 hours ago, ivan.popelyshev said:

OK, thought about that while i was sleeping. Finally got an answer:  its very good topic for book. As a result, we have books for 40$ and no open tutorials on physics integration :)

I dunno, it seems like you don't sleep! Thanks so much for the code example, it's helpful. I usually go for a slightly different route. I typically make graphic elements and physics elements components that get assigned to my game objects. 

BTW, in your constructor, when you position, shouldn't it be converting spaces (PTM)? I assume this is some code you already have working, but curious. I've never used p2, but I'm making the assumption that like box2d, one need to always convert spaces to get it to work right.

Link to comment
Share on other sites

I dunno, it seems like you don't sleep! 

I have thoughts even in my sleep. Controlled dreams stuff.

this.position is Body position, its in meters. MTP converts it to pixels and gives to pixi. Also it goes both ways: if pixi position was changed (localID thing) , we convert it to our body position, with PTM multiplication.

 

Link to comment
Share on other sites

  • 8 months later...
On 9/3/2019 at 9:01 PM, ivan.popelyshev said:

DONT USE PIXIJS WITH PHYSICS LIBRARIES. We tell that every time to every person who ask. But you guys are stubborn.

p2, matter, box2d, people use everything even when we forbid them to do so.

Whatever you use, those two libs can help with editing shapes for your physcis engine:

https://github.com/eXponenta/pixi-poly - integration with physics-editor

https://github.com/eXponenta/pixi-tiled - integration with tiled (shapes, not tilemap!)

Ivan, what should we do to make a game that requires physics? "DONT USE PIXIJS WITH PHYSICS LIBRARIES", that is, shouldn't we use pixi.js to make the game? I want to create something like this:

https://phaser.io/examples/v3/view/physics/matterjs/chain

don't we need physics library to build it?

 I'm sorry, I'm a beginner, and by reading your post, I had this question.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...