Collisions

I’m making a simple Pong game in a 3D space. I currently have the ball and two paddles created. I have created 2 float variables (bv_x and bv_y) that control the ball’s speed and direction. I want the ball to reverse it’s direction on the X-axis whenever it comes into contact with a paddle.



I have already confined the ball to a specific range on the Y-axis.

//===============================

Vector3f v = ball.getLocalTranslation();

if(v.y >= 82 || v.y <= -82) bv_y = -bv_y;

//===============================



And the paddles are already created using boxes.

//====================================

protected Geometry paddle1;

protected Geometry paddle2;

//====================================

Box paddle = new Box(3.0f, 15.0f, 15.0f);

//====================================

paddle1 = new Geometry(“blue paddle”, paddle);

paddle2 = new Geometry(“red paddle”, paddle);







How would I detect a collision between the ball and the paddles using the jMonkeyEngine? I’m certain I’ll need to use it’s bullet physics.

Hi,



it’s all perfectly explained in the tutorials, just invest an hour and read them :wink: You’ll need a BulletAppState, RigidBodyControllers, CollisionShapes and set your physics accordingly.



The collision detection will be handled by jME. If you don’t get the expected reverse direction after the collision you could write your own controller that overwrites/implements the onCollide() function ( I think that’s the name of the callback, don’t remember anymore :P).



And that’s it!

Thnx for the reply. I have looked into the tutorials and followed their instructions for collisions for quite a while already. The problem I ran into was the RigidBodyControllers. As I imported, it claimed that the RigidBodyControl class did not exist. I went ahead and looked into the jMonkey sources myself, and noticed that I had something called “PhysicsRagdollControl” rather than “RigidBodyControl”.





With that I changed this line:

//====================================

import com.jme3.bullet.control.RigidBodyControl;

//====================================



To:

//========================================

import com.jme3.bullet.control.PhysicsRagdollControl;

//========================================





Afterwards it imported properly, and I continued the program. The next problem arose when I attempted to use the “PhysicsRagdollControl”.





In the tutorials, they explained that the following code creates a box with Box Collision Shape:

//=====================================

Box myBoxx = new Box(1,1,1);

myBox.addControl(new RigidBodyControl( 1.0f ));

bulletAppState.getPhysicsSpace().add(myBox);

//=====================================



And so I modified it like this:

//=======================================

Box paddle = new Box(3.0f, 15.0f, 15.0f);

paddle.addControl(new PhysicsRagdollControl(1.0f));

bulletAppState.getPhysicsSpace().add(paddle);

//=======================================







But I get an error. In netbeans, it puts the red line under the ‘1.0f’. When I went to see what the error was, it told me:



“cannot find symbol

symbol : constructor PhysicsRagdollControl(float)

location: class com.jme3.bullet.control.PhysicsRagdollControl

paddle.addControl(new PhysicsRagdollControl(0.0f));”







And before you ask, yes, I created an object of the BulletAppState class after importing it.



//==============================

import com.jme3.bullet.BulletAppState;

//==============================

protected BulletAppState bulletAppState;

//==============================



And in the simpleInitApp() method:

//=============================

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

//=============================

Hi,



RigidBodyControl definitely has to exist. What is your jME3 version? I recommend using the current svn build or a fresh nightly build. If you don’t have the source class file then this means that maybe part of your jme3-bullet code is quite old.



BTW, your symbol error means that the constructor for one float cannot be found. Please update your jme3 and then we’ll se further!



Cheers

1 Like

Yeah, I noticed about the constructor error. I’ll try updating my jme3 right now. I’ll post again afterwards.

Alright, I went ahead and looked into the “sources” folder, and this newer version of jME3 contains RigidBodyControl. I should be able to finish my program now. I’ll post again after I get it working. Thank you for your help.

Well, I got everything working. I was having trouble using the bullet physics, but I was able to get my calculations done with simple bounding boxes. :stuck_out_tongue:

Hi,



glad to hear :slight_smile: Basically, bullet in conjunction with jME is very convenient to use and powerful. Maybe you just need to experiment a bit more!



How about a little demo of your project? :wink:



Cheers

Well, the ai, collisions, and scene are set up. How would I go about uploading a demo?

You can either use the desktop release formats (enable in project settings) which gives you nice zip packages for each platform or create a webstart version that can be downloaded and run from some webspace.

Well, here’s as far as I have gotten so far with it. It’s pretty much completed, I’d sill like to go back and see if I can add some music though.



http://www.gamefront.com/files/20030313/GONP3D.zip

Hehe,



nice little game. Fun to play! :slight_smile:



Two small things:

  1. You should make your game loop frame rate independent. Right now without VSync the game would just “fly by”. You have a variable called “tpf” in your update method which you can use to adjust your game speed to match frame rate.


  2. When deploying your game, make sure you remove all unnecessary lib-files in the lib folder. From 60mb right now you woud get down to maybe 5mb or something. Maybe this is something that could be configured when exporting with JMP, @normen?



    But good work! :slight_smile: Now on to the next one :stuck_out_tongue:



    Cheers

There is hints on how to reduce library size in the jMP manual.

Deleting the jme3testdata.jar from the lib directory reduces the game size to 10 MB,

Deleting jogl + jogl_natives and nifty-examples brings it down to 6 MB



Optimizing with ProGuard and then compressing via Pack200 + LZMA2 brings it to 1.6 MB :wink: