Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - nilotic

Pages: [1]
1
Support / How can i make 'Perfectly elastic collision'
« on: February 20, 2012, 01:42:45 pm »
Hello.
First, My English is not good, so I ask for your understanding regarding this matter.

I want to make 'perfectly elastic collision' between marble   in cube.
So, When collision occurs, I want to control the vector of marble.

But, I don't know How can i control the vector when collison occurs.

I already read all document about collision and sample Code, etc... (in JPCT Wiki).
And I'm testing in sample code. But, This code shows only slip between sphere.

Can i get some tips?

Code: [Select]
package com.threed.jpct.example;


import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
//import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

import com.threed.jpct.Camera;
//import com.threed.jpct.CollisionListener;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
//import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.MemoryHelper;



public class CollisionOfHelloWorld extends Activity {


//public class HelloWorld_col extends Activity {

   // Used to handle pause and resume...
   private static CollisionOfHelloWorld master = null;
   private GLSurfaceView mGLView;
   private MyRenderer renderer = null;
   private FrameBuffer fb = null;
   private World world = null;

   private Light sun = null;
   private RGBColor back = new RGBColor(50, 50, 100);


   private Object3D sphere1 = null;
   private Object3D sphere2 = null;

   
  // private SimpleVector move = new SimpleVector(0,0.3,1);
   private SimpleVector move2 = new SimpleVector(0, -1, 0);
  // private SimpleVector ellips = new SimpleVector(7,7,7);
   private SimpleVector tmp = new SimpleVector();
   private SimpleVector sunV = new SimpleVector(30,20, -150);
   

   protected void onCreate(Bundle savedInstanceState) {
      Logger.log("onCreate");

      if (master != null) {
         
      }

      super.onCreate(savedInstanceState);
      mGLView = new GLSurfaceView(getApplication());

      mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            // Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
            // back to Pixelflinger on some device (read: Samsung I7500)
            int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
            EGLConfig[] configs = new EGLConfig[1];
            int[] result = new int[1];
            egl.eglChooseConfig(display, attributes, configs, 1, result);
            return configs[0];
         }
      });

      renderer = new MyRenderer();
      mGLView.setRenderer(renderer);
      setContentView(mGLView);
   }

   

   protected void onStop() {
      Logger.log("onStop");
      super.onStop();
   }


   class MyRenderer implements GLSurfaceView.Renderer {

      private int fps = 0;
      private long time = System.currentTimeMillis();

      private boolean stop = false;

      public MyRenderer() {
         Config.maxPolysVisible = 500;
         Config.farPlane = 1500;
         Config.glTransparencyMul = 0.1f;
         Config.glTransparencyOffset = 0.1f;
         Config.useVBO=true;
         
         Texture.defaultToMipmapping(false);
         Texture.defaultTo4bpp(true);
      }

      public void stop() {
         stop = true;
      }

      public void onSurfaceChanged(GL10 gl, int w, int h) {
         if (fb != null) {
            fb.dispose();
         }

         fb = new FrameBuffer(gl, w, h);

         if (master == null) {
            world = new World();
                       
            sphere1 =  Primitives.getSphere(5);
            sphere1.setAdditionalColor(RGBColor.RED);
         
           
            sphere2 = sphere1.cloneObject();
            sphere2.setAdditionalColor(RGBColor.GREEN);
           
           
            sphere1.translate(0,-20,0);
            sphere2.translate(0, 40, 0);
           
           
           
            world.addObject(sphere1);
            world.addObject(sphere2);
         
                       
            // Setup collision modes
            sphere1.setCollisionMode(Object3D.COLLISION_CHECK_SELF | Object3D.COLLISION_CHECK_OTHERS);
            sphere2.setCollisionMode(Object3D.COLLISION_CHECK_SELF | Object3D.COLLISION_CHECK_OTHERS);
           
            world.setAmbientLight(20, 20, 20);
         
            sun = new Light(world);
            sun.setIntensity(250,250,250);
            sun.setPosition(sunV);
           
            world.buildAllObjects();

           
            Camera cam = world.getCamera();
            cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
            cam.moveCamera(Camera.CAMERA_MOVEUP, 60);
            cam.lookAt(sphere1.getTransformedCenter());

            MemoryHelper.compact();
           
         }
      }

      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
         
        // gl.glEnable(GL10.GL_BLEND);
         //gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
      }

      public void onDrawFrame(GL10 gl) {

         try {
            if (!stop) {
               
            // Do collision dections
            SimpleVector trsn = sphere2.checkForCollisionSpherical(move2, 5);
            sphere2.translate(trsn);
            Logger.log("Vector is "  + trsn);
           
           
            if (sphere2.getTranslation(tmp).z>100) {
            // Restart at the beginning...
            sphere2.clearTranslation();
            sphere2.translate(10, -40, -30);
            }else if (sphere2.getTranslation(tmp).y>100 || sphere2.getTranslation(tmp).y<-100) {
            // Restart at the beginning...
            sphere2.clearTranslation();
            sphere2.translate(0, 40, 0);
            }
           
               fb.clear(back);
               world.renderScene(fb);
               world.draw(fb);

               fb.display();

               fps++;
               
               if (System.currentTimeMillis()-time>=1000) {
               System.out.println(fps+"fps");
               fps=0;
               time=System.currentTimeMillis();
               }
            } else {
               if (fb != null) {
                  fb.dispose();
                  fb = null;
               }
            }
         } catch (Exception e) {
            e.printStackTrace();
            Logger.log("Drawing thread terminated!", Logger.MESSAGE);
         }
      }

     
   }

}

Pages: [1]