Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.50.0] Physics.Arcade.Body does not have a setDamping method #5352

Closed
juanitogan opened this issue Oct 11, 2020 · 8 comments
Closed

[3.50.0] Physics.Arcade.Body does not have a setDamping method #5352

juanitogan opened this issue Oct 11, 2020 · 8 comments

Comments

@juanitogan
Copy link

3.50.0-beta.8 Physics.Arcade.Body does not have a setDamping method. Workaround is to set useDamping yourself.

@photonstorm
Copy link
Collaborator

I'm not sure I'd call that a 'workaround' as it's exactly the correct way to set it!

@juanitogan
Copy link
Author

True enough. It is, however, surprising that when you switch from using physics.add.sprite() to physics.add.existing().body, for example, all the set methods still work except setDamping.

@photonstorm
Copy link
Collaborator

Thank you for submitting this feature request. We have implemented this and the feature has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

@mturneruk
Copy link

Hi There.

I am trying to create a geometry based game. I am a newb.

Please could explain "Workaround is to set useDamping yourself."

I want to apply damping to my "ship" which is at the moment just a triangle. I want to later create a whole bunch of geometry that will form the ship.

Cheers for any help.

@juanitogan
Copy link
Author

The answer to the question you are really asking (How do I build a composite object?) partly depends on the type of noob you are. Are you a noob like me who knows other game engines inside and out and is just trying to figure out Phaser? Are you a noob who doesn't know any game engines well but knows programming well (my real job is IT)? Or, are you a total noob who is still learning to program because you want to make games (like where I started)?

To me, Phaser doesn't appear to be targeted at composite objects. The parts are all there for it, I just haven't found much in the way of examples or docs for it, and this is also where the helper functions start to fall short and you have to fall back to objects and properties more. Thus, I've blazed my own way for building such objects. The key is obviously the Container object. But, what are the best practices for using it?

Here are some example excerpts from my code for my current project, Sendit Soccer, (language is ES6):

export function createBall (scene) {
	const obj = scene.add.container(0, 0)
...

	// Add the physics to container object and not the sprite.
	scene.physics.add.existing(obj, false).body
		.setCollideWorldBounds(true)
		.setCircle(5, -5,-5)
		.setBounce(0.8)
		.setDrag(0.98)
		//nope//.setDamping(true)
		.setMass(0.43)
	obj.body.useDamping = true //workaround

	obj.shadow = scene.add.image(0, 0, "ball-shadow")
	obj.add(obj.shadow)

	obj.sprite = scene.add.image(0, 0, "ball")
	obj.add(obj.sprite)

	return obj
}

or, as a class:

export default class Player extends Phaser.GameObjects.Container {
	constructor(scene, x, y, team, name) {
		//super(scene, x, y, children)
		super(scene, x, y)  // Note: super must be called before accessing "this".
		scene.add.existing(this)

		// Put the physics body on the container (vs the child sprite or something).
		scene.physics.add.existing(this, false).body
			.setCollideWorldBounds(true)
			.setCircle(10, -10,-10)

		// Add a child text game obj.
		this.name = scene.add.text( ... )
		this.name.setOrigin(0.5, 1.0)
		this.add(this.name)

		// Add a child sprite game obj.
		// (Seems silly to add it to the scene only to have the container
		//  remove it and add it to its own list.  But, alas....)
		this.sprite = scene.add.sprite(0, 0, key)
		this.add(this.sprite)
...

These, however, are objects with a single collider and multiple non-physics objects. I'm about to do the opposite next: a single sprite with multiple colliders as children to the container (which will be an interesting experiment considering Arcade allows only one collider per body). But, those will all be static bodies/colliders so it should be doable with enough wiring. I tried a second dynamic body on an object once and, well, it didn't work so well. It was always one frame behind. This was not a glitch. It makes sense for the game loop and Arcade physics (I would need to otherwise control the child directly like I do the parent body... possible, but gets messy quickly).

This brings me to your real question. Arcade physics doesn't sound like what you ultimately want. I have another 2D game built in Unity and Box2D, GRITS Racing, with cars that are built from a complex array of objects. The wheels are separate from the body and other parts, applying thrust and steering to the composite object. (With different art, the car could easily be a space ship with four thrusters -- which would actually be easier because then I wouldn't have to model forward grip vs lateral grip vs downforce vs body roll vs various surfaces, etc.) If I were to port that game to Phaser, I would surely be using Matter physics to replace Box2D, and not Arcade.

The point is, if you want a collection of objects to accelerate and collide as a whole, you need a physics engine that has Joint objects to make sure all the joined objects know how to move together. I haven't used Matter yet, but it looks like the kind of engine that can do this. Thus, depending on what type of noob you are, and how much you like to jump into the deep end to learn to swim (which is my usual m.o.), your game design may be too big. Otherwise, good luck and I hope I've helped you (and anyone else reading this) a bit.

@mturneruk
Copy link

Wow. Thankyou for taking the time to give such a detailed answer. Brilliant.

I did manage to get useDamping to work, but I am finding that its overly complicated in phaser 3 to create basic geometry and apply physics to them.

What I absolutely need to learn first is ES6. Something I have never done. I am old school programmer and i have never managed to break the habit of coding in functions. I realise that games have to be written classes like your examples, so that you can have a class like enemies or bullets.

I looked at matter yesterday, and decided that was where i needed to start. You can draw primitives with that as well by the looks of it, so that's where i am going to start.

Cheers for your very detailed answer.

@juanitogan
Copy link
Author

My first computer (more or less) was a Vic 20, so you're not the only old schooler around here. I'm mostly a folk programmer and, yeah, procedural as well. OOP is useful, but it has its limits too.

I can't say you need to learn ES6. I still target browsers that aren't fully ready for it yet and so I use Babel to translate the ES6 back to plain ol' JS. Getting a Babel env up (with Browserify, Watchify, and the other dozen things you need) is an enigma and I can't say it's worth it just to take Phaser for a spin. I have Babel set up for some of my contract work so it was an easy choice for me here.

Then again, I'm not an ES6-vs-browser expert, so I can't tell you if now is a good time to move to ES6 without Babel. If you're okay with just supporting the latest browsers, it looks fine according to this: http://kangax.github.io/compat-table/es6/

Good luck with that ship. I hope you find something that works.

@mturneruk
Copy link

mturneruk commented Oct 24, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants