Raggar Posted July 31, 2016 Share Posted July 31, 2016 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? Quote Link to comment Share on other sites More sharing options...
JohnK Posted August 1, 2016 Share Posted August 1, 2016 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. Quote Link to comment Share on other sites More sharing options...
Raggar Posted August 1, 2016 Author Share Posted August 1, 2016 Hmm. I tried implementing it, but no matter what, it only shoots in One direction. The tank will be moving at all times, so the direction has to be calculated dynamically. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted August 1, 2016 Share Posted August 1, 2016 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 Raggar 1 Quote Link to comment Share on other sites More sharing options...
V!nc3r Posted August 1, 2016 Share Posted August 1, 2016 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 ? Quote Link to comment Share on other sites More sharing options...
adam Posted August 1, 2016 Share Posted August 1, 2016 1 hour ago, aWeirdo said: the bullet starting position needs a bit of tweaking though. http://www.babylonjs-playground.com/#2IRUGC#7 Raggar and aWeirdo 2 Quote Link to comment Share on other sites More sharing options...
Raggar Posted August 2, 2016 Author Share Posted August 2, 2016 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. 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/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.