|
-
January 14th, 2011, 01:25 AM
#1
Need help regarding game logic
Hi to all,
Hope you all will be fine. I am making a cricket game and now i am stuck at a point when i am trying to do a ball. Actually what happen , that when ball strikes with the ground(floor) it continues to bounce on the same point. I want that it creates a effect that after bounce the ball goes to the batsman. I define
bouncing point. Here is my code to implement bounce. Please help.
Code:
public class DemoGameCanvas extends GameCanvas implements Runnable {
//360 degrees = 2 * pi radians
// 360 / 6.28 = 1 radian
// 57. 3 degress = 1 radian
private static final int FP_PI2 = MathFP.mul(MathFP.PI, MathFP.toFP(2)); // pi * 2 = 6.2832
private static final int FP_DEGREES_PER_RADIAN = MathFP.div(MathFP.toFP(360), FP_PI2); // 360 / 2*pi = 1 radian
private static int SPRITE_IMAGES_WIDTH = 67;
private static int SPRITE_IMAGES_HEIGHT = 137;
private static int BALL_SPRITE_IMAGE_WIDTH = 5;
private static int BALL_SPRITE_IMAGE_HEIGHT = 5;
private int ballXFP = MathFP.toFP(140); // x position (as a MathFP)
private int ballYFP = MathFP.toFP(100); // y position (as a MathFP)
private int direction = 270 ; // current direction in degrees
private int xVelFP; // x velocity (as a MathFP)
private int yVelFP; // y velocity (as a MathFP)
private int maxVelFP = MathFP.toFP("2");
private int thrustFP = MathFP.toFP("0.1"); // decreasing this value will cause greater bounce
private int ballOrigXFP;
private int ballOrigYFP;
public DemoGameCanvas() {
super(true);
try {
//setFullScreenMode(true);
init();
} catch (IOException ioe) {
ioe.printStackTrace();
}
} //DemoGameCanvas()
/**
* Initialize the Game Design, then load all layers and start animation threads.
*/
private void init() throws IOException {
//Background image
cricketGround = gameDesign.getCrik();
//For all sprites positions
lm = new LayerManager();
gameDesign.updateLayerManagerForMatchGround(lm);
ballImage = gameDesign.getBall5px();
ballSprite = gameDesign.getWhiteBall();
ballSprite.defineReferencePixel(3, 3);
.......
} //init()
public void run() {
Graphics g = getGraphics();
while (!interrupted) {
updateScreen(g);
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} //run()
private void updateScreen(Graphics g) {
createBackGround(g);
moveBall();
ballSprite.setRefPixelPosition(MathFP.toInt(ballXFP), MathFP.toInt(ballYFP));
playShots();
}
private void createBackGround(Graphics g) {
lm.paint(g, 0, 0);
flushGraphics(0, 0, getWidth(), getHeight());
}
private void moveBall() {
// Wrap the direction around if it's now invalid.
if (direction < 0) {
direction = 359;
}
if (direction > 359) {
direction = 0;
}
//save the original position (so if it collided we can back out of this movement)
ballOrigXFP = ballXFP;
ballOrigYFP = ballYFP;
/******
* Converting degrees into radian
* 360 degrees = 2*pi radians
* 360 degrees = 6.28 radians
* 360 / 6.28 degrees = 1 radian
* 57.3 degrees = 1 radian
* 1 degree = 1 / 57.3 radians
* 45 degrees = 45 / 57.3 radians
*/
//move the ball according to its current direction (radians)
// if direction is 270 then dirRadian = 4.7124
int dirRadians = MathFP.div(MathFP.toFP(direction), FP_DEGREES_PER_RADIAN); //direction / 57.3
//Calculate the accelaration for this cycle by multiplying the direction
//by the thrust on both x and y
int xAccFP = MathFP.mul(thrustFP, MathFP.cos(dirRadians)); // Vox = Vo * cos(thete)
int yAccFP = MathFP.mul(thrustFP, -MathFP.sin(dirRadians)); // Voy = Vo * sin(theta)
//increase the velocity by the amount of accelaration in this cycle
xVelFP = MathFP.add(xVelFP, xAccFP);
yVelFP = MathFP.add(yVelFP, yAccFP);
//cap our velocity to a controllable level. Capping both positive and negative velocity
//Without this, the ball would soon be moving at breakneck speed
if (xVelFP > maxVelFP) { //+ve x velocity
xVelFP = maxVelFP;
}else if (xVelFP < -maxVelFP) { //-ve x velocity
xVelFP = -maxVelFP;
}
if (yVelFP > maxVelFP) {
yVelFP = maxVelFP;
}else if (yVelFP < -maxVelFP) {
yVelFP = -maxVelFP;
}
// //Move ths ship according to its current velocity
ballXFP = MathFP.add(ballXFP, xVelFP);
ballYFP = MathFP.add(ballYFP, yVelFP);
// //add the xcomponent of the movement (cos radian)
// ballXFP = MathFP.add(ballXFP, MathFP.cos(dirRadians));
//
// //add the y component of the movement (negative sin radian). We use negative
// //sign to convert from cartesian to screen co-oedinate
// ballYFP = MathFP.add(ballYFP, -MathFP.sin(dirRadians));
//check our position and if we're hitting the floor reverse a velocity component
if (MathFP.toInt(ballXFP) + BALL_SPRITE_IMAGE_WIDTH >= getWidth()) {
//back out of the collision position
ballXFP = ballOrigYFP;
xVelFP = MathFP.mul(xVelFP, MathFP.toFP("-1.0"));
}
if (MathFP.toInt(ballYFP) + BALL_SPRITE_IMAGE_HEIGHT >= getHeight() - 130) {
//back out of the collision positin
ballYFP = ballOrigYFP;
yVelFP = MathFP.mul(yVelFP, MathFP.toFP("-1.0"));
}
// Check our position and wrap around to the other side of the canvas
// if we have to.
if (MathFP.toInt(ballXFP) < viewPortX) {
ballXFP = MathFP.toFP(getWidth()-1);
}
if (MathFP.toInt(ballXFP) > getWidth()) {
ballXFP = MathFP.toFP(viewPortX);
}
if (MathFP.toInt(ballYFP) < viewPortY) {
ballYFP = MathFP.toFP(getHeight() - 1);
}
if (MathFP.toInt(ballYFP) > getHeight()) {
ballYFP = MathFP.toFP(viewPortY);
}
} moveBall()
} // class DemoGameCanvas
please help how can i implement the logic that after bounce the ball goes to batsman instead of bouncing at the same point.
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|