Java-Gaming.org Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision  (Read 2055 times)
0 Members and 1 Guest are viewing this topic.
Online Kefwar
« Posted 2014-01-22 19:12:11 »

How do you check movement collisions, I've got some nice-looking objects but I can just "fly" with my camera through them, and through my floor too. Does anyone know good tutorials/samples?

Sneak-preview Cheesy

My Steam profile: Kefwar
Offline Drenius
« Reply #1 - Posted 2014-01-22 19:16:34 »

You managed to make this in Java but you have no idea of collision detection?
Offline Longor1996
« Reply #2 - Posted 2014-01-22 19:19:07 »

A plan:
- Try to make your own.
- Realise its too hard.
- Use JBullet! Its easy.

...

If you want to roll your own, make yourself ready for a heck-load of math, physics, and linear algebra.
Seriosly, just go and use JBullet for collision detection and handling.
It makes your life a lot easier.

Have a nice day!

- Longor1996

PS: I can't see the image. It's WAY too big for my internet connection (0-64 Bit per Second).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Kefwar
« Reply #3 - Posted 2014-01-22 19:23:01 »

Thanks for the advice Cheesy, I'll look for JBullet.

My Steam profile: Kefwar
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 1040
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2014-01-23 20:26:12 »

Either a physics library, or write code to handle sphere/sphere collisions, and compose a tight fitting bounding volume around your objects using a bunch of (never rendered) spheres. Then you can proudly bump into your trees.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Online Kefwar
« Reply #5 - Posted 2014-01-24 14:29:34 »

What is the best way to translate my VBO which I load in LWJGL to a CollisionShape of the JBullet library?

My Steam profile: Kefwar
Offline Opiop
« Reply #6 - Posted 2014-01-24 14:35:43 »

That's the most vague request ever. What is a CollisionShape? Are you using a library? What have you tried? What is the problem?

Online Kefwar
« Reply #7 - Posted 2014-01-24 15:34:08 »

I load objects from a Wavefront (.OBJ) file, and create a model of them.
1  
2  
3  
4  
5  
6  
    // List of vertices
    private List<Vector3f> vertices;
    // List of normals
    private List<Vector3f> normals;
    // List of faces
    private List<Face> faces;

I want to create a CollisionShape in the JBullet library for this object. What would be the best way to do this? I can't even find an idea how to do this.

My Steam profile: Kefwar
Offline Opiop
« Reply #8 - Posted 2014-01-24 15:47:22 »

Did you follow theCodingUniverse's tutorials to do this? If you did, I know he also had tutorials on JBullet.

Online Kefwar
« Reply #9 - Posted 2014-01-24 16:35:04 »

I've got it a bit working, but it's reallllly glitchy:
1  
2  
3  
4  
    public static CollisionShape modelToShape(ObjectArrayList<Vector3f> points) {
       CollisionShape shape = new ConvexHullShape(points);
       return shape;
    }

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
    public static RigidBody createRigidBody(CollisionShape shape, float mass, float restitution, Vector3f pos, Vector3f rotation) {
        DefaultMotionState motionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(rotation.x, rotation.y, rotation.z, 1), new Vector3f(pos.x, pos.y, pos.z), 1)));
        Vector3f inertia = new Vector3f();
        shape.calculateLocalInertia(mass, inertia);
        RigidBodyConstructionInfo constructionInfo = new RigidBodyConstructionInfo(mass, motionState, shape, inertia);
        constructionInfo.restitution = restitution;
        RigidBody body = new RigidBody(constructionInfo);
        body.setCollisionFlags(CollisionFlags.STATIC_OBJECT);
        return body;
    }


But the floor is everywhere the same height and it doesn't work at all with the trees.

My Steam profile: Kefwar
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zFollette

Junior Devvie


Exp: 2 years


I like jokes


« Reply #10 - Posted 2014-01-24 22:23:39 »

Did you follow theCodingUniverse's tutorials to do this? If you did, I know he also had tutorials on JBullet.

He has a tutorial on JBullet, but it doesn't really cover that, I don't think.

EDIT: I was horribly wrong.

Humor will keep you alive.
Online Kefwar
« Reply #11 - Posted 2014-01-26 15:30:14 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
    public static CollisionShape modelToShape(List<Vector3f> vertices, List<Face> faces) {
       float[] coords = new float[vertices.size()*3];
       for(int n = 0; n < vertices.size(); n++) {
          Vector3f f = vertices.get(n);
          coords[n*3] = f.x;
          coords[n*3+1] = f.y;
          coords[n*3+2] = f.z;
       }
       int[] indices = new int[faces.size()*3];
       for(int n = 0; n < faces.size(); n++) {
          Face f = faces.get(n);
          indices[n*3] = (int) f.getVertex().x;
          indices[n*3+1] = (int) f.getVertex().y;
          indices[n*3+2] = (int) f.getVertex().z;
       }
   
      if (indices.length > 0) {
         IndexedMesh indexedMesh = new IndexedMesh();
         indexedMesh.numTriangles = indices.length / 3;
         indexedMesh.triangleIndexBase = ByteBuffer.allocateDirect(indices.length*4).order(ByteOrder.nativeOrder());
         indexedMesh.triangleIndexBase.asIntBuffer().put(indices);
         indexedMesh.triangleIndexStride = 3 * 4;
         indexedMesh.numVertices = coords.length / 3;
         indexedMesh.vertexBase = ByteBuffer.allocateDirect(coords.length*4).order(ByteOrder.nativeOrder());
         indexedMesh.vertexBase.asFloatBuffer().put(coords);
         indexedMesh.vertexStride = 3 * 4;
   
         TriangleIndexVertexArray vertArray = new TriangleIndexVertexArray();
         vertArray.addIndexedMesh(indexedMesh);
   
         boolean useQuantizedAabbCompression = true;
         BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(vertArray, useQuantizedAabbCompression);
         
         return meshShape;
      }
       
       return null;
    }


I'm getting this stack trace:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
Exception in thread "main" java.lang.IndexOutOfBoundsException
   at java.nio.Buffer.checkIndex(Unknown Source)
   at java.nio.DirectByteBuffer.getFloat(Unknown Source)
   at com.bulletphysics.collision.shapes.ByteBufferVertexData.getVertex(ByteBufferVertexData.java:58)
   at com.bulletphysics.collision.shapes.VertexData.getTriangle(VertexData.java:53)
   at com.bulletphysics.collision.shapes.StridingMeshInterface.internalProcessAllTriangles(StridingMeshInterface.java:51)
   at com.bulletphysics.collision.shapes.StridingMeshInterface.calculateAabbBruteForce(StridingMeshInterface.java:78)
   at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.<init>(BvhTriangleMeshShape.java:76)
   at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.<init>(BvhTriangleMeshShape.java:63)
   at tk.Kefwar.OpenGL.Physics.modelToShape(Physics.java:117)
   at tk.Kefwar.OpenGL.model.Model.prepareVBO(Model.java:257)
   at tk.Kefwar.OpenGL.model.Model.loadOBJModel(Model.java:187)
   at tk.Kefwar.OpenGL.model.Scenario.loadObjects(Scenario.java:84)
   at tk.Kefwar.OpenGL.model.Scenario.<init>(Scenario.java:21)
   at tk.Kefwar.OpenGL.Main.init(Main.java:37)
   at tk.Kefwar.OpenGL.util.Game.gameLoop(Game.java:77)
   at tk.Kefwar.OpenGL.util.Game.<init>(Game.java:37)
   at tk.Kefwar.OpenGL.Main.<init>(Main.java:14)
   at tk.Kefwar.OpenGL.Main.main(Main.java:142)


Anyone knows what I'm doing wrong?

My Steam profile: Kefwar
Offline Opiop
« Reply #12 - Posted 2014-01-26 15:34:49 »

No one is going to help unless you are more specific and show effort yourself. You have an index out of bounds exception, go ahead and try to figure out what's going out of bounds. Simple debugging.

Online Kefwar
« Reply #13 - Posted 2014-01-26 16:18:40 »

I got the code above working, by just increasing vertices.size() *3 to vertices.size() *4. And it works.

My Steam profile: Kefwar
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 162
Projects: 5
Exp: 3 years



« Reply #14 - Posted 2014-01-26 17:53:20 »

I got the code above working, by just increasing vertices.size() *3 to vertices.size() *4. And it works.
Hopefully you know why?
Online Kefwar
« Reply #15 - Posted 2014-02-04 15:55:41 »

I have been implementing this to my game, and I discovered a problem: My character was rolling off the hills when I used a RigidBody as camera/character. I have searched a lot and saw about the KinematicCharacterController. After several attempts and failures I don't get it working.
Main source I've used: http://stackoverflow.com/questions/18405592/jbullet-kinematic-objects.

My CharacterController class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
package tk.Kefwar.OpenGL.Physics;

import javax.vecmath.Vector3f;

import tk.Kefwar.OpenGL.model.Texture;

import com.bulletphysics.collision.broadphase.CollisionFilterGroups;
import com.bulletphysics.collision.dispatch.CollisionFlags;
import com.bulletphysics.collision.dispatch.GhostObject;
import com.bulletphysics.collision.dispatch.PairCachingGhostObject;
import com.bulletphysics.collision.shapes.ConvexShape;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.character.KinematicCharacterController;
import com.bulletphysics.linearmath.Transform;

public class CharacterController {
   private DynamicsWorld world;
    private MyMotionState myMotionState;
    private RigidBody rigidBody;
    private KinematicCharacterController character;
    private ConvexShape shape;
    private PairCachingGhostObject ghost;
    private Vector3f pos;
   
    public CharacterController(DynamicsWorld world, Vector3f initialPosition, ConvexShape shape, MyMotionState myMotionState) {
       this.pos = initialPosition;
        this.world = world;
        RigidBodyConstructionInfo constructInfo = new RigidBodyConstructionInfo(50.0f, myMotionState, shape);
        this.myMotionState = myMotionState;
        rigidBody = new RigidBody(constructInfo);
        ghost = new PairCachingGhostObject();
        ghost.setCollisionShape(shape);
        ghost.setCollisionFlags(CollisionFlags.CHARACTER_OBJECT);
        character = new KinematicCharacterController(ghost,shape,1);
        world.addCollisionObject(ghost, CollisionFilterGroups.CHARACTER_FILTER, (short)(CollisionFilterGroups.STATIC_FILTER | CollisionFilterGroups.DEFAULT_FILTER));
        world.addAction(character);
    }
    public void move(float dx, float dy, float dz, float yaw, float pitch) {
        pos.z += dx * (float) Math.cos(Math.toRadians(yaw)) + dz *        Math.cos(Math.toRadians(yaw));
        pos.x -= dx * (float) Math.sin(Math.toRadians(yaw)) + dz * Math.sin(Math.toRadians(yaw));
        pos.y += dy * (float) Math.sin(Math.toRadians(pitch)) + dz * Math.sin(Math.toRadians(pitch));
        //pseudocode
        Transform transform = new Transform();
        transform.origin.set(pos.x, pos.y, pos.z);
        rigidBody.setWorldTransform(transform);
    }
    public Vector3f getPosition() {
       return pos;
    }
}


But now I come to my question: I'm back to the start of the thread, when I move this object, it doesn't check for collision and gravity isn't working anymore. What am I doing wrong?

My Steam profile: Kefwar
Offline Longor1996
« Reply #16 - Posted 2014-02-04 19:01:45 »

I think the thing with the rolling can be stopped by:
character.setAngularMovement( 0 );

Online Kefwar
« Reply #17 - Posted 2014-02-04 19:05:57 »

I think the thing with the rolling can be stopped by:
character.setAngularMovement( 0 );

The rolling was when using a RigidBody and synchronise the camera position with that body and not a KinematicCharacterController of the code I posted. With the controller physics don't work at all for me.

My Steam profile: Kefwar
Offline Genius Coder

Junior Devvie


Medals: 1
Exp: 4 years



« Reply #18 - Posted 2014-02-04 19:07:32 »

That's the most vague request ever. What is a CollisionShape? Are you using a library? What have you tried? What is the problem?

To say you have quite a high rank your not very observant. He quite clearly states what he's having trouble with

I love making games...
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 1040
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #19 - Posted 2014-02-04 19:18:24 »

Everybody makes mistakes. No need to rub it in. Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Online Kefwar
« Reply #20 - Posted 2014-02-04 19:29:51 »

I just tried it at another way: no KinematicCharacterController, but now I removed all the forces on the x and z axis, and it is nearly what I want, the only problem is that collision with objects like walls pushes me up instead of back. Is there a way to let only those collision forces apply and not things like gravity rolling me off the hills. I move the camera RigidBody by changing it's world transform.
1  
2  
3  
4  
5  
6  
7  
8  
//The method to move the camera
public void move(float amount, float direction) {
   com.bulletphysics.linearmath.Transform transf = body.getWorldTransform(new com.bulletphysics.linearmath.Transform());
   javax.vecmath.Vector3f vec = transf.origin;
   vec.z -= amount * Math.sin(Math.toRadians(rotation.y + 90 * direction));
   vec.x -= amount * Math.cos(Math.toRadians(rotation.y + 90 * direction));
   body.setWorldTransform(transf); //body is the RigidBody of the camera
}

And this updates the physics:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
    public void updatePhysics(float delta) {
        RigidBody camera = Main.getCamera().getRigidBody();
       Vector3f cp = camera.getWorldTransform(new Transform()).origin; //The current CameraPosition
        dynWorld.stepSimulation(delta, 10);
        Transform np = camera.getWorldTransform(new Transform()); // The new CameraPosition
        np.origin.x = cp.x;
        np.origin.z = cp.z;
       
        camera.setWorldTransform(np);
        camera.setLinearVelocity(new Vector3f(0,camera.getLinearVelocity(new Vector3f()).y,0)); // Clears all the linear vilocity except for the Y axe
        Vector3f pos = camera.getWorldTransform(new Transform()).origin;
        Main.getCamera().setPosition(-pos.x, -pos.y, -pos.z);
    }

My Steam profile: Kefwar
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Coldstream24 (17 views)
2015-09-03 00:41:28

Andrew_3ds (28 views)
2015-09-01 19:08:10

afikri (19 views)
2015-08-31 09:30:22

afikri (26 views)
2015-08-31 09:30:07

afikri (13 views)
2015-08-31 09:27:24

afikri (17 views)
2015-08-31 09:26:40

Roquen (30 views)
2015-08-29 11:30:54

GamerC4 (37 views)
2015-08-22 20:38:50

GamerC4 (34 views)
2015-08-22 20:37:18

GamerC4 (41 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!