Тема: JBullet завантажує проц.
Хай. Чого в мене так грузиться проц, коли в сцені лишень декілька сфер?
▼Прихований текст
▼Прихований текст
import com.bulletphysics.BulletGlobals;
import com.bulletphysics.ContactAddedCallback;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.broadphase.BroadphaseProxy;
import com.bulletphysics.collision.broadphase.CollisionFilterGroups;
import com.bulletphysics.collision.broadphase.DbvtBroadphase;
import com.bulletphysics.collision.dispatch.CollisionConfiguration;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.CollisionFlags;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.narrowphase.ManifoldPoint;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.collision.shapes.StaticPlaneShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.InternalTickCallback;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.constraintsolver.ConstraintSolver;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import com.jogamp.opengl.util.*;
import com.jogamp.opengl.util.gl2.GLUT;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
public class Main implements GLEventListener {
private double theta = 0;
private double s = 0;
private double c = 0;
private GLU glu;
private GLUT glut;
private GL2 gl;
private static double y;
private static float cameraPosX,cameraPosY=5,cameraPosZ=-100, cameraRotX,cameraRotY,cameraRotZ,
mouseSpeedX,mouseSpeedY,mouseLastPosX,mouseLastPosY,
cameraAngleX,cameraAngleY,cameraAngleZ,ballPosition;
/*JBullet*/
private static DynamicsWorld dynamicsWorld;
private static Set<RigidBody> balls = new HashSet<RigidBody>();
private static RigidBody groundRigidBody,ballRigidBody;
private static Transform groundTransform;
private static MotionState ballMotionState;
private static CollisionShape ballShape;
private static void setUpPhysics()
{
BulletGlobals.setContactAddedCallback(new ContactAddedCallback() {
@Override
public boolean contactAdded(ManifoldPoint manifoldPoint, CollisionObject collisionObject, int i, int i2,
CollisionObject collisionObject2, int i3, int i4) {
System.out.println("collide!");
return false;
}
});
BroadphaseInterface broadphase = new DbvtBroadphase();
CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
ConstraintSolver solver = new SequentialImpulseConstraintSolver();
dynamicsWorld=new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld.setGravity(new Vector3f(0,-9.81f,0));
CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0,1,0),0.25f);
groundShape.setLocalScaling(new Vector3f(1,1,1));
ballShape = new SphereShape(1);
groundTransform=new Transform(new Matrix4f(new Quat4f(0,0,0.0f,1),new Vector3f(0,0,0),1.0f));
MotionState groundMotionState = new DefaultMotionState();
RigidBodyConstructionInfo groundBodyConstructionInfo = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0,0,0));
groundBodyConstructionInfo.restitution=0.25f;
groundRigidBody = new RigidBody(groundBodyConstructionInfo);
groundRigidBody.setCollisionFlags(groundRigidBody.getCollisionFlags() | CollisionFlags.KINEMATIC_OBJECT);
dynamicsWorld.addRigidBody(groundRigidBody);
ballShape = new SphereShape(1);
ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),new Vector3f(0,40,0),1.0f)));
RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape, new Vector3f(0,0,0));
ballShape.calculateLocalInertia(1.0f,new Vector3f(0,0,0));
ballBodyConstructionInfo.restitution=0.5f;
ballBodyConstructionInfo.angularDamping=0.95f;
ballRigidBody = new RigidBody(ballBodyConstructionInfo);
ballRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
balls.add(ballRigidBody);
dynamicsWorld.addRigidBody(ballRigidBody);
}
private static void AddBall()
{
SphereShape ballShape = new SphereShape(1);
MotionState ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),new Vector3f(0,40,0),1.0f)));
RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape, new Vector3f(0,0,0));
ballShape.calculateLocalInertia(1.0f,new Vector3f(0,0,0));
ballBodyConstructionInfo.restitution=0.5f;
ballBodyConstructionInfo.angularDamping=0.95f;
RigidBody ballRigidBody = new RigidBody(ballBodyConstructionInfo);
ballRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
balls.add(ballRigidBody);
dynamicsWorld.addRigidBody(ballRigidBody);
}
public static void StartGL()
{
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
final GLCanvas canvas = new GLCanvas(caps);
Frame frame = new Frame("AWT Window Test");
frame.setSize(1024, 720);
frame.add(canvas);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 38) {
y += 0.1;
}
if (e.getKeyCode() == 40) {
y -= 0.1;
}
switch (e.getKeyChar()) {
case 'w':
cameraPosZ += 1f;
cameraRotZ += 1f;
break;
case 's':
cameraPosZ -= 1f;
cameraRotZ -= 1f;
break;
case 'a':
cameraPosX += 1f;
cameraRotX+=1f;
break;
case 'd':
cameraPosX -= 1f;
cameraRotX-=1f;
break;
case 'e':
cameraPosY += 1f;
cameraRotY+=1f;
break;
case 'q':
cameraPosY -= 1f;
cameraRotY-=1f;
break;
case ']' :
Transform transform=new Transform();
ballMotionState.getWorldTransform(transform);
transform.origin.setX(transform.origin.x-1);
ballMotionState.setWorldTransform(transform);
ballRigidBody.setMotionState(ballMotionState);
break;
case '[' :
Transform transform1=new Transform();
ballMotionState.getWorldTransform(transform1);
transform1.origin.setX(transform1.origin.x+1);
ballMotionState.setWorldTransform(transform1);
ballRigidBody.setMotionState(ballMotionState);
break;
case 'f' :
AddBall();
}
}
});
canvas.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
mouseLastPosX=e.getX();
mouseLastPosY=e.getY();
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB),new Point(0,0),"Hide"));
}
@Override
public void mouseReleased(MouseEvent e) {
canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
canvas.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
mouseSpeedX=e.getX()-mouseLastPosX;
mouseSpeedY=e.getY()-mouseLastPosY;
mouseLastPosX=e.getX();
mouseLastPosY=e.getY();
cameraAngleX+=mouseSpeedX/5;
cameraAngleY+=mouseSpeedY/5;
cameraRotX=(float)(Math.sin(Math.toRadians(cameraAngleX)))+cameraPosX;
cameraRotY=(float)(Math.sin(Math.toRadians(cameraAngleY)))+cameraPosY;
cameraRotZ=(float)(Math.cos(Math.toRadians(cameraAngleX)))+cameraPosZ;
System.out.print(Math.toRadians(cameraAngleX)+ " : " + cameraAngleY+"\n");
}
@Override
public void mouseMoved(MouseEvent e) {
}
});
canvas.addGLEventListener(new Main());
FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.start();
}
public static void main(String[] args) throws Exception{
setUpPhysics();
StartGL();
}
@Override
public void init(GLAutoDrawable glAutoDrawable) {
glu=GLU.createGLU(glAutoDrawable.getGL().getGL2());
glut=new GLUT();
gl=glAutoDrawable.getGL().getGL2();
// gl.glPolygonMode(gl.GL_FRONT_AND_BACK,gl.GL_LINE);
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display(GLAutoDrawable glAutoDrawable) {
update();
render(glAutoDrawable);
}
private void render(GLAutoDrawable drawable)
{
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.2f, 0.4f, 0.7f, 0);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(cameraPosX, cameraPosY, cameraPosZ, cameraRotX, cameraRotY, cameraRotZ, 0, 1, 0);
for(int i =0; i<balls.size()+1; i++)
{
gl.glPushMatrix();
}
int x=0;
for(RigidBody ball : balls)
{
gl.glPopMatrix();
Vector3f ballPosition = ball.getWorldTransform(new Transform()).origin;
gl.glTranslatef(ballPosition.x,ballPosition.y,ballPosition.z);
gl.glColor3f(0, 1, 0);
glut.glutSolidSphere(1,100,100);
x++;
}
gl.glColor3f(0.5f,0,0);
gl.glPopMatrix();
float[] m= new float[16];
groundRigidBody.getWorldTransform(new Transform()).getOpenGLMatrix(m);
FloatBuffer matrix = FloatBuffer.wrap(m);
gl.glMultMatrixf(matrix);
gl.glBegin(gl.GL_POLYGON);
gl.glVertex3f(-50,0,-50);
gl.glVertex3f(-50,0,50);
gl.glVertex3f(50,0,50);
gl.glVertex3f(50,0,-50);
gl.glEnd();
}
private void update()
{
theta += 1;
c = Math.cos(theta);
dynamicsWorld.stepSimulation(1.0f / 60.0f);
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int i, int i2, int w, int h) {
gl.glViewport(0,0,w,h);
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(40,(float)w/h,0.1f,1000);
gl.glMatrixMode(gl.GL_MODELVIEW);
}
}