d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Eurgh.. Pool Game. - Java > Java Pool Game
Add Reply New Topic New Poll
Member
Posts: 25,132
Joined: Sep 22 2007
Gold: 0.00
Apr 27 2016 07:30am
Created a game of pool with a basic game engine...

I have initial cue -> cue ball movement working on mouse click, drag and release using the following code:

where ball[0] is the cue ball, g1 is the instance of the game engine.

Code

double currentPosX = (balls[0].getXPosition());
double currentPosY = (balls[0].getYPosition());
double mouseSpeedX = (g1.getFirstClickX() - g1.getSecondClickX());
double mouseSpeedY = (g1.getFirstClickY() - g1.getSecondClickY());

balls[0].setXPosition(currentPosX+mouseSpeedX);
balls[0].setYPosition(currentPosY+mouseSpeedY);


However... the movement is static i.e. it will move the ball[0] from point A to point B... I know why, because its only setting the value but i don't know how i can change it...

I want the movement to be fluid so i thought if i devide both the x and y changes by like 20 then set the movements to increase in increments of that value whilst subtracting that value from mouseSpeedX and mouseSpeedY until they reach 0???

opinions / help would be appreciated :)
Member
Posts: 7,324
Joined: Dec 22 2002
Gold: 1,261.00
Apr 28 2016 03:01pm
You'll need a physics engine for something like pool. Writing up all the physics yourself would be overwhelming. With a physics engine you'll just impart an impulse to the ball in a certain direction and the engine will appropriately figure out the ball's position over time, given a certain coefficient of friction with the table surface and other balls.

If you really want you can try to fake it by very quickly incrementing the ball's position in small amounts, but it will be difficult to make it look good. Especially considering that you want the ball to move quickly at first and slow down over time. I would suggest grabbing something like PhysX or Bullet and working with that.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll