Jump to content

setLinearVelocity on LOCAL Space?


xtreemze
 Share

Recommended Posts

Hello Everyone,

I've been trying to go at this on my own but I've been stuck for days trying to add some basic AI to a cube.

Play the FPS style game here on desktop or mobile: https://xtreemze.github.io/Ballistic you can find the full JavaScript here: https://github.com/xtreemze/Ballistic/blob/master/js/master.js

Controls: touch the cube button on the bottom to shoot ice cubes and use standard universal camera controls to move and look around.

Here's where I'm stuck:

I got window.cube to jump and look at the camera but now I need a force to push the window.cube to move towards window.camera. I just can't figure out how to setLinearVelocity in the local forward direction of window.cube.

redMesh is the Mesh of window.cube and redCube is the impostor of redMesh.

(Also would be nice if the lookAt() rotation was eased or slowed down so it wouldn't seem so sudden but that's a lower priority.)

Here's the part of the code that controls the cube AI and thanks in advance for taking a shot at this!

// Global Scope Declarations for Console Tests
window.button = button;
window.cube = redCube;
window.cubeMesh = redMesh;
window.camera = camera;
// Red Cube turns to look at camera
const rotateCube = function rotateCube() {
  redMesh.lookAt(camera.position);
};
// Cube Movement towards Camera ???
const jump = new BABYLON.Vector3(0, 5, 0);
const front = new BABYLON.Vector3(0, -10, 0);
const goForward = redMesh.getDirection(front);
const moveCube = function moveCube() {
  redCube.setLinearVelocity(jump);
  rotateCube();
  // redCube.setLinearVelocity(goForward);
};
window.setInterval(moveCube, 2000);

 

Link to comment
Share on other sites

Hello!

pretty nice game.

I will suggest using impulse instead of setLinearVelocity. Impulse is great because it apply a force to a given object at the given coordinate:

http://doc.babylonjs.com/overviews/using_the_physics_engine#impulses-and-forces

To get the forward facing vector from the camera just call camera.getForwardRay().direction

 

 

Link to comment
Share on other sites

Thanks for the quick reply @Deltakosh! I really like the Babylonjs engine you've helped to make and I'm using this little game as a prototype to learn more about it.

1 hour ago, Deltakosh said:

Hello!

pretty nice game.

I will suggest using impulse instead of setLinearVelocity. Impulse is great because it apply a force to a given object at the given coordinate:

http://doc.babylonjs.com/overviews/using_the_physics_engine#impulses-and-forces

To get the forward facing vector from the camera just call camera.getForwardRay().direction

 

 

I will try to use impulse instead but I'm confused with what vectors to use as arguments because the forward face of the camera is irrelevant in this case because the red cube (redMesh) should approach the camera at all times even if the camera is looking away. The game already does this, but I'd like to use physics instead of using locallyTranslate(). It would be more helpful to acquire the forward facing vector of the "enemy" red cube and then apply the force onto that cube so the position of the cube approaches the camera with each jump. getForwardRay() is useful for shooting the projectiles, however. But it seems I can't use it for the enemy cube because getForwardRay() seems available to the camera and not the abstract mesh.

To rephrase my question and follow your suggestion to use impulse: How can I pass the local forward facing vector of the redMesh cube to an impulse that would apply a force of 2 along the axis facing the camera global position?

Link to comment
Share on other sites

On 8/3/2017 at 3:06 AM, Deltakosh said:

This should be something like:


var direction = camera.position.subtract(redCube.position);

direction.normalize()

 

Thanks @Deltakosh

Had to play around with it a bit but I think this is what you were suggesting and it works!:

https://www.babylonjs-playground.com/indexstable#UXU2E9

 

I'm just now learning to calculate two vectors together... I'll apply it to the game soon once I get the force to hit the cube a bit lower than the central position so the cube doesn't fall on it's forward face when it jumps.

Link to comment
Share on other sites

Hi guys.  Xtreemze... cool project/playground.  Frog attack!  :)

I want to show you a func, x.  Perhaps it is useless for your project, but perhaps not.

https://www.babylonjs-playground.com/debug#PBVEM#157

Big fat PG for flying a friggin' white box, eh?  CONTROL + arrows/pgUp/pgDown for rots, SHIFT + arrows/pgUp/pgDown for trans.  Control-Shift "L" for all stop (currently broken, perhaps).  Numpad active, too.  HOLDING keys is common - this is a small-pulse momentum-accumulation thrust system - it utilizes standard held-keypress repeating.

Lines 157-162 is the func I want to show.  It is an "aimer", in a way.  It ensures that no matter HOW the white box is rotated, a translate +X (for example)... always moves the white box in LOCAL space.  If the craft were inverted, a translatePositiveY command would send the flyer DOWNWARD.  Lefts would go right, rights would go left, etc, etc.

Scroll down a bit until you hit lines 191 and 194 in the rotPositiveX func.  See the doTransformPerFlyerQuat() being called?  Needless to say, it is a useful func for me, in that demo.

I use that doTransformPerFlyerQuat() for all my trans and rot funcs.  It allows my future impulsings to be "synced-up" with the current box rot (using Quaternion rotation, which is what the physics engines use for their rots).

Btw, I apologize for the dual-impulsing rotation methods.  I coded this BEFORE I knew about impostor.setAngularVelocity()  :)

All in all, I wanted to show you this func, and show you how I used it.  I hug this function every single day... it is one of my best friends.   :D

Link to comment
Share on other sites

2 minutes ago, Wingnut said:

Hi guys.  Xtreemze... cool project/playground.  Frog attack!  :)

I want to show you a func, x.  Perhaps it is useless for your project, but perhaps not.

https://www.babylonjs-playground.com/debug#PBVEM#157

Big fat PG for flying a friggin' white box, eh?  CONTROL + arrows/pgUp/pgDown for rots, SHIFT + arrows/pgUp/pgDown for trans.  Control-Shift "L" for all stop.  Numpad active, too.

Lines 157-162 is the func I want to show.  It is a "aimer", in a way.  It ensures that no matter HOW the white box is rotated, a translate +X (for example)... always moves the white box in LOCAL space.  If the craft were inverted, a translatePositiveY command would send the flyer DOWNWARD.  Lefts would go right, rights would go left, etc, etc.

Scroll down a bit until you hit lines 191 and 194 in the rotPositiveX func.  See the doTransformPerFlyerQuat() being called?  Needless to say, it is a useful func for me, in that demo.

I use that doTransformPerFlyerQuat() for all my trans and rot funcs.  It allows my future impulsings to be "synced-up" with the current box rot (using Quaternion rotation, which is what the physics engine use for their rots).

Btw, I apologize for the dual-impulsing rotation methods.  I coded this BEFORE I knew about impostor.setAngularVelocity()  :)

All in all, I wanted to show you this func, and show you how I used it.  I hug this function every single day... it is one of my best friends.   :D

Thanks for checking out my game/prototype @Wingnut! I didn't realize it but those cubes do seem like frogs jumping :D

That's a yuge PG you got there! Quaternions are next on my ToDo list so this should be useful for some future projects I'd like to work on once I get the hang of the basics. Thanks for sharing! By the way, I also made a taxi game as one of my first javascript/HTML5 endeavours https://xtreemze.github.io/TAXi/:D

Link to comment
Share on other sites

I updated the game and used this code in case anyone is wondering how the code turned out.

window.up = 130;
window.forward = 93;
    const moveCube = function moveCube() {
      redMesh.lookAt(camera.position);
      const direction = camera.position.subtract(redMesh.position);
      direction.normalize();
      // Speed control
      const result = direction.scale(window.forward);
      // Jump
      result.y = window.up;
      const bottomForce = redMesh.position.subtract(new BABYLON
        .Vector3(0, 2, 0));
      redCube.applyImpulse(result, bottomForce);
    };
    window.setInterval(moveCube, 2000);

 

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...