Search the Community

Showing results for tags 'force'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • HTML5 Game Coding
    • News
    • Game Showcase
    • Facebook Instant Games
    • Coding and Game Design
  • Frameworks
    • Phaser 3
    • Phaser 2
    • Pixi.js
    • Babylon.js
    • Panda 2
    • melonJS
    • Haxe JS
    • Kiwi.js
  • General
    • General Talk
  • Business
    • Collaborations (un-paid)
    • Jobs (Hiring and Freelance)
    • Services Offered

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start



Website URL





Found 9 results

  1. splash27

    Wind Implementation

    Hi, everyone! I want to share my wind implementation in BabylonJS with explanations. It may to seem obvious. Wind is just an adding the vector to objects position. But if we'll go this way, then result will be boring. We'll just get sliding objects. Our goal is to perform the storm with bouncing and collisions. First of all we need to enable collisions for all meshes and camera. We' ll also configure gravity here. scene.gravity = new BABYLON.Vector3(0, -0.9, 0); scene.collisionsEnabled = true; camera.checkCollisions = true; camera.applyGravity = true; camera._needMoveForGravity = true; ground.checkCollisions = true; box.checkCollisions = true; c1.checkCollisions = true; c2.checkCollisions = true; c3.checkCollisions = true; Then we need to implement a kind of objects pushing. When camera can pushes objects. I decided to mark meshes that can be pushed by camera with special property "pushable". Here is implementation. var physicsPlugin = new BABYLON.CannonJSPlugin(); scene.enablePhysics(); ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.9 }, scene); box.physicsImpostor = new BABYLON.PhysicsImpostor(box, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 10, restitution: 0.1 }, scene); c1.physicsImpostor = new BABYLON.PhysicsImpostor(c1, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 20, restitution: 0.1 }, scene); c2.physicsImpostor = new BABYLON.PhysicsImpostor(c2, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 20, restitution: 0.1 }, scene); c3.physicsImpostor = new BABYLON.PhysicsImpostor(c3, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 150, restitution: 0.1 }, scene); //handling when camera pushes other meshes var playerImpulse = 30; //we can use fixed value, because player mass and velocity is constant in most cases camera.onCollide = function (mesh) { if (mesh.pushable) { var givenVelocity = playerImpulse / mesh.physicsImpostor.mass; var camPosition = camera.position.clone(); camPosition.y -= camera.ellipsoid.y * 0.80; //kick height var movementDirectionVector = mesh.getAbsolutePosition().subtract(camPosition); mesh.physicsImpostor.setLinearVelocity(movementDirectionVector.scale(givenVelocity)); } }; Ok, when we "touch" a mesh we detect if it's pushable, then calculate direction using camera and mesh positions. Thanks @Wingnut for tips with this part. Also, I correct direction at y property. It needed to make things look like the player applies force using his leg. So I correct the height at which the force will be applied. I don't use addImpulse method, because CannonJS accumulate impulses and we can boost meshes to incredible velocities by several touches. It's better to set the final velocities. And now the wind. We need some things to keep in mind to procced. Wind should preserve current meshes velocities. It should not to override, for example, a gravity effect. Wind should be applied to camera also for best user's experience. But we know that camera has now physicsImpostor. We must provide safe places, where we can hide from the wind, for example, in a cave or behind the building. Should work for meshes and camera. And now the implementation goes. var wb1 = BABYLON.MeshBuilder.CreateBox("wb1", {width: 8, height: 8, depth: 8}, scene); var wbMat = new BABYLON.StandardMaterial("wbmat", scene); wbMat.wireframe = true; wb1.position = new BABYLON.Vector3(55, -6, 2); wb1.material = wbMat; var wb2 = BABYLON.MeshBuilder.CreateBox("wb2", {width: 8, height: 8, depth: 8}, scene); wb2.position = new BABYLON.Vector3(40, -6, 20); wb2.material = wbMat; function pointIntersectsAny(volumes, point) { for (var k = volumes.length - 1; k >= 0; k--) { if (volumes[k].intersectsPoint(point)) { return true; } } return false; } function meshIntersectsAny(volumes, mesh) { for (var k = volumes.length - 1; k >= 0; k--) { if (volumes[k].intersectsMesh(mesh, false)) { return true; } } return false; } //wind processor, should be called from gaming loop function processWind (scene, camera, vector, meshWindScale, windBlocks) { var scale = meshWindScale || 1; var windBlocks = windBlocks || []; if (!pointIntersectsAny(windBlocks, camera.position)){ camera.position = camera.position.add(vector); } var meshes = scene.meshes; for (var i = meshes.length - 1; i>=0; i--) { if (meshes[i].pushable && meshes[i].physicsImpostor) { if (!meshIntersectsAny(windBlocks, meshes[i])) { meshes[i].physicsImpostor.setLinearVelocity(meshes[i].physicsImpostor.getLinearVelocity().add(vector.scale(scale))); } } } } Ok, wb1 and wb2 are safe volumes where is no wind at any conditions. I use a wireframe to display, later these meshes can be set as invisible. pointIntersectsAny and meshIntersectsAny are helpers and pretty self-explanatory. The main action is inside processWind function. We handle the camera and meshes separately. Actually, we don't see the player outside of the camera, so, we can manipulate it's position directly. What concerns the meshes, we should apply linearVelocity to them to make CannonJS to handle their movements in physical manner. Look how we preserved the initial linearVelocity using getLinearVelocity. Of cource the wind can't move the heavy objects if wind's strength is not big enough. If a camera or meshes are inside the safe volume then no wind will be applied to it. I also added an ability to scale the wind applied to meshes. It could be useful for some effects. For example, when player has a special boots with high friction value. So the player can resist the wind. Also important to say, that wind could be applied only to "pushable" meshes that have physicsImpostors. And all we need is just launch whe wind. var windAllowed = false; //press "f" to launch the wind document.addEventListener("keydown", function (e) { if (e.keyCode == 70) { windAllowed = true; } }); scene.registerBeforeRender(function () { if (windAllowed) { processWind(scene, scene.cameras[0], new BABYLON.Vector3(0.1, 0, 0.1), 2, [wb1, wb2]); } }); Here is playground: I also implemented jumping, croaching and free mouse-look just for fun. =) Yeah, there are still some issues. For example, camera affected by the wind can flow through objects. It could be fixed by checking onCollide event. Another issue, this wind acts like constant force. It looks like spaceship depressurization than like actual wind. But you can try to add flurries using f(t) = |six(t)| function or something else. Feel free to tune my approach for your needs or share your implementations. If you want to use this in your project, the credits will be much appreciated but it's not required.
  2. Olliebrown

    Phaser P2 Physics - Apply Force

    I am attempting to apply force manually to a sprite that has a P2 physics body. My hope is to achieve the designable jump described by Kyle Pitmann in his GDC Talk below. In this he derives equations for a jump arc that are controlled by the height, footspeed, and how long the jump should last. It will give you a gravitational force and an initial velocity to achieve those parameters. Everything I do to try and set a manual force on the P2 body is being ignored. Here is what I have: Gravity on the body is disabled via = 0 I then try and initiate a jump using my precomputed initial velocity and gravitational constant as follows: let vx = config.WALK_SPEED sprite.body.force.y = this._jump_grav_start * vx * vx sprite.body.velocity.y = this._jump_vel_start * vx Afterwards I dump 'force' to the console and it is always zero no matter what I try to set it to. Am I just missing something fundamental here about how you apply a force in P2? Seth B. )
  3. jacquesr

    Forces acting at an angle

    Hello everyone, I started programming motion calculation to get into math and physics a little deeper again. I know that my problems have already been solved by other people around the globe every day, but I don't want to use the simple approach with a physics engine for now. I want to understand how things work. So. Here is my problem: I have an entity with mass and velocity and it is currently heading 0. Now, I apply a side force (centripetal force). It might come from any angle. My current implementation uses a standard centripetal force calculation using a circle, where I get the radius from mass and velocity and can tell where my new point is, given the amount of time that has passed between two frames. Yet, in theory, that is not the correct calculation, if we delete the time aspect. Why? because my force direction is absolute. I am not in a car where I apply angle to wheels so I would go in a circle forever. I want to go e. g. to 90deg while currently, I am heading to 0. The centripetal force calculation with the circle only works approximately and while we assume that the framerate is high enough so we don't slip through the 90° point (because e. g. at 89, where the centripetal force amount is only 1deg, if enough time passes, the next point in the circle could be more than 90deg relative to the starting point where the force was initially applied). It could then potentially go back and forth a little. So what I would need is a spline that is correct at the current time and then I can set the relative time that has passed and that gives me a new point where I am. Of course, when my angle changes, I need a new spline. But as long as no parameters are changed, I need to have a calculated spline on which I can move. But silly me, I did only do middle school and I have never been in touch with such calculations. Can someone help me with that? I hope my problem is clear. See the attached screenshot.
  4. I have a p2 Object with the following properties, true); this.player.body.clearShapes(); this.player.body.loadPolygon('playerPolygon','player'); this.player.body.allowRotation = false; this.player.body.fixedRotation = true; this.player.body.immovable = true; this.player.body.setCollisionGroup(playerCollision); this.player.body.collides(ballCollision); this.player.body.kinematic = true;[0].sensor = false; this.player.body.collideWorldBounds = true; The above object can only move along its X axis and I want to trigger push/ hit force to its impacting object on collision; this.ball.body.setCircle(28); this.ball.body.collideWorldBounds = true; this.ball.body.allowRotation = true; this.ball.body.fixedRotation = false; this.ball.body.adjustCenterOfMass(); this.ball.body.setCollisionGroup(ballCollision); this.ball.body.collides(playerCollision); The above object is always the impacting object whose movement is controlled by gravity and impulse. I'm trying to figure out how to calculate the following:- The point of collision on the body of "this.player" The impulse to apply to the impacting object after collision to simulate a proper/ accurate deflection setting the projected angle and speed/ velocity what properties of the impacting object do I set? Force? AngularVelocity? Velocity?
  5. I am trying to create a little game but i have this problem (for now ) : At the "create" state i define this.collisionCircle = new Phaser.Circle(0,0,2);this.playerRectangle = new Phaser.Rectangle (0,0,0,0);this.player = game.add.sprite(,,'player');game.physics.arcade.enable(this.player);this.player.body.drag.set(550);this.circleCenter = new Phaser.Point(0,0);this.PointToGo = new Phaser.Point(0,0);At the "update" step i havecollisionCircle.setTo(mycircle.center_x,mycircle.center_y,50);if ((Phaser.Circle.intersectsRectangle(collisionCircle,playerRectangle)) && (touchesPlayerAlready==false)){ touchesPlayerAlready=true; circleCenter.setTo(mycircle.center_x,mycircle.center_y); PointToGo=Phaser.Point.rotate(circleCenter, playerRectangle.centerX, playerRectangle.centerY, 180, true); game.physics.arcade.accelerateToXY(player,PointToGo.x,PointToGo.y,20)} What i am trying to do is using the circular sprite to "push" the player sprite. I define the coordinates of PointToGo as the diametrically opposite point of the circle center using the player sprite coordinates as the middle point of the rotation. My problem is that, even setting the Drag property of player, the player sprite after the first intersection with the circular sprite does not decreases it's velocity but it always ends it's movement at the "borders" of the game (defined with a tilemap). What i want to do is simulate a real-life push, something that at the beginning has an acceleration, caused by a force, but needs to have a deceleration of it's velocity caused by friction. Maybe i am doing it wrong here by using the accelerateToXY method but i cannot find something like applyForce in arcade physics. Can anyone help me with this ?
  6. onedkr

    Weird collision with p2 physics

    Hi everyone, I have one problem, I'm a beginner with Phaser and I just passed my game with p2 physics. I have three sprite : the floor and 2 different player. Each player have to collide together and with the floor of course. The two players collide correctly with the ground. However when the player 1 collides with the player 2 (or inverse) and I continue to move the player to the other player's direction, the first one pushes the second player. When I stop the first player is push backward. I just want that the first player cannot move if he touch the second player (like a wall). How is it possible ? I tried to reset all the force and velocity parameters, but that doesn't works... initialize group collision : playerCollisionGroup = game.physics.p2.createCollisionGroup();enemyCollisionGroup = game.physics.p2.createCollisionGroup();groundCollisionGroup = game.physics.p2.createCollisionGroup();game.physics.p2.updateBoundsCollisionGroup();Here my code for the ground : ground = game.add.sprite(, GAME_HEIGHT - 55, 'ground');ground.scale.setTo(40, 1);game.physics.p2.enable(ground, true);ground.body.static = true;ground.body.setCollisionGroup(groundCollisionGroup);ground.body.collides([playerCollisionGroup, enemyCollisionGroup]);For each player I initialize like this : function initBody(carac) { if( == "player") body = carac.create( - 300, GAME_HEIGHT - 255 - (GAME_HEIGHT - ground.body.y), 'body'); else if( == "enemy") { body = carac.create( + 320, GAME_HEIGHT - 250 - (GAME_HEIGHT - ground.body.y), 'body'); body.scale.x = -1; } body.body.setZeroForce(); body.body.setZeroVelocity(); body.body.setZeroDamping(); body.body.angularForce = 0; body.body.angularVelocity = 0; body.body.angularDamping = 0; body.body.fixedRotation = true; body.animations.add('normal', [0,1]); body.animations.add('jump', [0]); body.animations.add('move', [2]); // Set the body collision group if( == "player") { body.body.setCollisionGroup(playerCollisionGroup); body.body.collides([groundCollisionGroup, enemyCollisionGroup]); } else if( == "enemy") { body.body.setCollisionGroup(enemyCollisionGroup); body.body.collides([groundCollisionGroup, playerCollisionGroup]); } return body;} Any idea ?Thank you very much for your answers
  7. ekstrakt

    Apply force to sprite

    Hi all, I'm struggling a bit to find a way to make a realistic explosion effect (physics, not visual). The situation is this: The game is a horizontal side-scrolling platformer. I have a player that's throwing grenades. On impact with a tile, the grenade explodes. I want all enemies (sprites) within the blast to be affected by a realistic circular blast wave, pushing the enemies away in relation to how close they were to the impact point. I have the impact vector (distance and angle from impact point), and I can calculate the impact force (closest - max force, further - less force) in relation to the blast radius, but so far I have no means to apply it to an enemy. Some ways I can think of: - push the enemy back by setting it's x,y, which is instantly, and not realistic - apply (add) negative/positive velocity to the enemy (which will be constant, and unrealistic) - create many concentric circles with center at the impact point, and over time degrade their velocity effect to 0, while having a onOverlap callback adding the velocity effect to the enemy sprite - create a circle at the explosion point, which will expand by time, and push out the sprites by collision (maybe add bounce to the enemies). But unless I use P2 physics, this will be unrealistic - using - Phaser.Physics.Arcade.accelerateToObject(enemy, blastOrigin, -500, 0, 0) - Phaser.Physics.Arcade.accelerateToXY(enemy, PointFromForce, 500, 0, 0) - Phaser.Physics.Arcade.moveToObject(enemy, blastOrigin, -500, 0, 0) - Phaser.Physics.Arcade.moveToXY(enemy, PointFromForce, 500, 0, 0) but with all these methods the enemy continues to move indefinitely, and I don't have a way to stop it What I'm trying to achieve is like 2 sprites colliding (with bounce), with a finite stop point, or a planet with inverse gravity (pushing bodies away). Best aproach I can think of at the moment is to have something like deccelerateToXY(Point) function, which will stop the sprite when the point is reached. At the moment I'm using Arcade physics, but if this can be done with P2, I will switch. Also, note that, at the moment, the enemies don't move by themself, but when I'm done with implementing the AI, they will move (try to follow the player, run away from player, move independently ...), which will bring additional complications. Any ideas and help are appreciated. P.S. Can I mix Arcade and P2 physics?
  8. kkaczor

    Adding force to a moving sprite

    Hi! I have got sprite which is moving with constant speed using: moveLeft Now I want to apply force to it to simulate effect of pushing it away from player. I tried: zombie.sprite.body.velocity.x = -100;zombie.sprite.body.velocity.y = -100;but the x component seems to be ignored - I think that i because of moveLeft in another part of a update loop. How I suppose to deal with it?
  9. yegorf1

    Slow moving in P2.

    Holla, amigos! How to slow moving of bodies in P2 physics? I need to substrat 1 pts from body speed per second.