Code Structure Recommendations for a Tank Game

I would like to make a tank game using Heaps, where the tank remains stationary, but the player can control the rotation of the tank’s turret and cannon.

I have a 3D model that I made in Blender.

The thing I am unsure of, is how to code the tank rotation behavior in Haxe/Heaps. So far, I have brought the model into the Heaps world

    function createTank() {
      var cache = new h3d.prim.ModelCache();
      tank = cache.loadModel(hxd.Res.Tank1);

I have been toying around with rotating the tank with tweening using Actuate

    function updateTankPosition(az: Float, el: Float, pow: Float) {
        var targetAz = degToRad(az);
        Actuate.update(tank.setRotation, 2, [0, 0, lastAz], [0, 0, targetAz]);
        lastAz = targetAz;

There are three parts of the tank. Hull, Turret, and Cannon. The issue I have at the moment is that I only want two out of the three parts to rotate, but I have no idea what the best way to do this would be.

I want the cannon to act as a child of the turret, and rotate together with the turret on the Z axis. Then I want the cannon to rotate independently on the X azis relative to the turret.

I made some armature bones in Blender as seen in the first screenshot. This was just an idea but I’m not married to this idea. The idea was to animate individual bones in Heaps, but I’m not seeing a clear-cut way to do that. I also had the idea of creating a turret rotation animation in Blender, and triggering that animation in Heaps using h3d.scene.playAnimation(), pausing the animation, and setting it to a specific frame to match the desired angle. That seems kind of clunky to me and only addresses one of the two rotations that I desire. I’d like to think there’s a better way.

How do you think I should go about achieving this motion in Heaps engine?

I’m not an expert with Heaps but looking at the source I see that calling the h3d.prim.ModelCache::loadModel() method returns a h3d.scene.Object instance.

That class has methods you can use to find child objects by name, so if you have your Turret and Cannon objects named in Blender, and that naming successfully carries over to heaps on load, then you should be able to query the scene graph to get those sub objects. Then you’ll be able to manipulate each object individually.

Now if you built your tank in Blender so that the Cannon is a child of the Turret, then rotating the Turret should be sufficient to rotate them both, assuming you have the parent/child relationships and anchors setup correctly.

So check out that class for its query methods and go from there.

1 Like