Jump to content

Shoot Bullet in Specific Direction?


Raggar
 Share

Recommended Posts

I'm sure there's a simple solution to this issue.

I'm trying to shoot a bullet from the turret of a tank, in the direction that the turret is pointing.

I made this simple scene. to demonstrate what I mean:

http://www.babylonjs-playground.com/#2IRUGC#2

 

As you can see, I created a very basic tank, and placed a bullet (sphere) just behind the turret. In front of the turret is a Box mesh( bPoint1 ).

I was hoping I could somehow shoot the bullet in the direction of the bPoint, in order for it to "appear" to be shot from the turret.

I made the turret spin ever so slightly.

 

How should I approach this?

Link to comment
Share on other sites

Assumption - the tank does not fire while the gun is rotating.

Something along the following lines should work but you will probably need absolutePosition rather than position as parenting is involved.

bulletDirection = bpoint.position.subtract(sphere.position);
bulletDirection.normalize();
startPosition = sphere.position;
t = 0;

In render loop

sphere.position = startPosition.add(bulletDirection.scale(t));
t += 0.1 //or some appropriate value

 Hope this gives you help to proceed.

Link to comment
Share on other sites

Hi @Raggar

check this out :) http://www.babylonjs-playground.com/#2IRUGC#5

What i did was, i made an array where a new bullet gets added on every click, the new bullets clone the rotation and position of the top of the tank and that is then used to calculate the direction and starting position.
In the renderloop it then runs through the array and moves the bullets using the rotation.y as direction .  tried getting it to work with a registerBeforeRender but it kept erroring so i just threw it in the renderLoop instead..

 

the bullet starting position needs a bit of tweaking though.

and note that it doesn't delete anything from the array, so it just keeps building bigger and bigger and thus slower and slower with every click.. easy to fix with a lifetimer on each bullet as it is being made though just add another variable to the bullet creation, could be something like " bullets[bulletId].lifetimer = 1000; ", and on every renderloop run " bullets.lifetimer -= 1; " and a " if (bullets.lifetimer < 0) bullets.dispose(true); " or something like that.

oh, and sorry i broke your pretty this. style :D 

Link to comment
Share on other sites

Maybe it's possible to use a physic engine ? I don't know how it could be use in BJS, but it seems (in my n00b-physic-opinion) that you'll juste have to apply a force in the right direction and the physic engine do the ballistics. And maybe it's possible to set the force-field child of the turret ?

Link to comment
Share on other sites

Awesome. Works like a charm. Thanks!

I just spent 3 hours implementing it into my own base-game, and I get some weird results like wrong directions, only 1 bullet, and bullet travel only when clicking.

But these are my issues to figure out. :P

 

 

20 hours ago, V!nc3r said:

Maybe it's possible to use a physic engine ? I don't know how it could be use in BJS, but it seems (in my n00b-physic-opinion) that you'll juste have to apply a force in the right direction and the physic engine do the ballistics. And maybe it's possible to set the force-field child of the turret ?

I had this one bookmarked:

http://jafty.com/3D/game/

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