Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get an objects parameter with collision detection?
    primarykey
    data
    text
    <p>I started working on a game with jMonkey. I just created an object from the class "Entity" I made myself which holds an 3D Model, Physics and so on. Here is the code:</p> <pre><code>package mygame.entities; import com.jme3.asset.AssetManager; import com.jme3.bullet.BulletAppState; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.renderer.queue.RenderQueue.ShadowMode; import com.jme3.scene.Geometry; import com.jme3.scene.Node; import com.jme3.scene.Spatial; public class Entity { private AssetManager assetManager; private Node rootNode; public Spatial model; private Geometry object; private String itsName; private int life; private boolean destroyAble; private boolean destroyed; public Entity(BulletAppState bas, AssetManager manager, Node rootNode, String name, int lifes, boolean destroyable, float x, float y, float z) { itsName = name; life = lifes; destroyAble = destroyable; model = manager.loadModel("Models/woodlog.j3o"); model.setLocalTranslation(x, y, z); model.setShadowMode(ShadowMode.Cast); model.setName(name); model.setUserData("lifes", 3); RigidBodyControl body = new RigidBodyControl(2); model.addControl(body); bas.getPhysicsSpace().add(body); rootNode.attachChild(model); } public String getName() { return itsName; } public int getLife() { return life; } public void setLife(int lifes) { life = lifes; } public boolean isDestroyable() { return destroyAble; } public boolean isDestroyed() { if (destroyAble &amp;&amp; life &lt;= 0) { destroyed = true; } else { destroyed = false; } return destroyed; } } </code></pre> <p>With help from the tutorial on the jMonkey website I managed to implement the "shooting". A simple ray that follows my cam direction. Here is the code for what happens if it collides with something:</p> <pre><code>} else if (binding.equals("Fire") &amp;&amp; !isPressed) { // 1. Reset results list. CollisionResults results = new CollisionResults(); // 2. Aim the ray from cam loc to cam direction. Ray ray = new Ray(cam.getLocation(), cam.getDirection()); // 3. Collect intersections between Ray and Shootables in results list. shootables.collideWith(ray, results); // 4. Print results. System.out.println(results.size()); if (results.size() &gt;= 1) { System.out.println(results.getCollision(0).getGeometry().getName()); //Material material = results.getCollision(0).getGeometry().getMaterial(); //material.setColor("Color", ColorRGBA.randomColor()); } } </code></pre> <p>So that works just fine! This line:</p> <pre><code>System.out.println(results.getCollision(0).getGeometry().getName()); </code></pre> <p>displays the name of that "Geometry" i just shot. But the problem now is, that my object is not a Geometry! And I dont know how I can implement that I get the name of this object anyway. The best way for me would be if the results.getCollision(0) would return my object so I could just say "object.getName();"</p> <p>Does anyone know how I can do that? I would be very grateful for any ideas :) cheers - Daniel </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload