[SOLVED] Fireball attack implementation

Hello dear community,

I made a nice fireball particle effect. The player shall be able to throw these fireballs. The fireball shall “fly” into the direction the player is looking at in a straight line. Furthermore, the fireball can hit other players but it also can collide with the terrain etc.

The thing is I am not sure how I should implement this. Has somebody done something similar like this already?

My first thought would be to create a sphere collision shape and make it static to be not affected by gravity and then move it every frame. Then I also could easily check for collision, right?

Best regards
Domenic

I am doing it exactly like this, or comparable.
I have a model for the core I think (which is currently a tin can :smiley: ) and for this I have a custom collision listener which makes the fireball explode if it collides.

But don’t make it static, just set it’s impulse and wait for collision feedback.

I was looking in the wiki about collisions since I figured that there must be something specifically used for bullets and the like. This seems to be more or less what you’re looking for no? https://jmonkeyengine.github.io/wiki/jme3/advanced/collision_and_intersection.html#toolbar

Rather this
https://jmonkeyengine.github.io/wiki/jme3/advanced/physics_listeners.html#physics-collision-listener

Oh! Yes use this. Not what I posted. Sorry.

This one even has a small list saying that you shouldn’t use what I posted for what you’re doing. My bad.

Alright, thanks for your quik answers.

Could you please tell me how I should setup the physics control then? If I don’t make it static (mass = 0) it will be affected by gravity, right?

Unless you remove its gravity.

1 Like

Hello pspeed, sorry if this sounds stupid or anything but I could not find a way to “remove” the gravity of a single physics object such as my firball. Setting the gravity to (0,0,0) does not have any effect. I also could not find anything in the wiki, sorry. Could you please help me one more time?

So without looking at your code I had a similar issue with this. [Solved] Class that extends RigidBodyControl and gravity issues - #6 by thecyberbob

Essentially I had set it how I thought I needed to but the objects kept falling through the floor.

1 Like

Did you set the gravity before or after you added it to the physics space and it applied the default gravity?

2 Likes

RigidBodyControl.setGravity(new Vector3f(0f,0f,0f));
See: [https://jmonkeyengine.github.io/wiki/jme3/advanced/physics.html#specify-physical-properties]

1 Like

Ok, maybe this helps. Here is the code

package de.gamedevbaden.crucified.tests;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import de.gamedevbaden.crucified.enums.Scene;


public class FireballTest extends SimpleApplication {

    public static void main(String[] args) {
        new FireballTest().start();
    }

    @Override
    public void simpleInitApp() {
        flyCam.setMoveSpeed(30);
        BulletAppState bulletAppState = new BulletAppState();
        bulletAppState.setDebugEnabled(true);

        stateManager.attach(bulletAppState);

        bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0,-9.81f, 0));
        Spatial scene = assetManager.loadModel(Scene.GameLogicTestScene.getScenePath());
        RigidBodyControl scenePhysicsControl = new RigidBodyControl(CollisionShapeFactory.createMeshShape(scene), 0);
        bulletAppState.getPhysicsSpace().add(scenePhysicsControl);
        scene.addControl(scenePhysicsControl);
        rootNode.attachChild(scene);

        AmbientLight light = new AmbientLight();
        light.setColor(ColorRGBA.White);
        rootNode.addLight(light);

        inputManager.addMapping("FIRE", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(new ActionListener() {
            @Override
            public void onAction(String name, boolean isPressed, float tpf) {
                if (!isPressed)return;

                Spatial fireball = assetManager.loadModel("Models/Effects/Fireball.j3o");
                fireball.setLocalTranslation(cam.getLocation());
                rootNode.attachChild(fireball);

                RigidBodyControl fireControl = new RigidBodyControl(CollisionShapeFactory.createBoxShape(fireball), 1);
                fireball.addControl(fireControl);
                fireControl.setGravity(new Vector3f(0,0,0));
                bulletAppState.getPhysicsSpace().add(fireControl);

                FireballCollisionListener listener = new FireballCollisionListener(fireControl);
                bulletAppState.getPhysicsSpace().addCollisionListener(listener);

                fireControl.setLinearVelocity(cam.getDirection().mult(10));
            }
        }, "FIRE");

    }

    private class FireballCollisionListener implements PhysicsCollisionListener {

        private RigidBodyControl fire;

        private FireballCollisionListener(RigidBodyControl fire) {
            this.fire = fire;
        }

        @Override
        public void collision(PhysicsCollisionEvent event) {
            if (event.getObjectA() == fire || event.getObjectB() == fire) {
                System.out.println("EXPLOSION!");
            }
        }
    }
}

Alright, yeah, my mistake. I have to swap those two lines here

fireControl.setGravity(new Vector3f(0,0,0));
bulletAppState.getPhysicsSpace().add(fireControl);

to

bulletAppState.getPhysicsSpace().add(fireControl);
fireControl.setGravity(new Vector3f(0,0,0));

Thank you all for the great help!

2 Likes

Hint to future readers:
This is because adding an object to a physics space sets it’s gravity to the space’s gravity.

6 Likes