Search code examples
java-megame-physics

related to Marble Maze Game


I am working on the project of marble maze game in j2me.I am facing the problem related to the movement and controlling of the ball.

The code i am using is

 private SensorConnection sensor;
 Data[] data;
 double value[] = new double[3];
 double PreValueX1, PreValueX2, PreValueY1, PreValueY2;
 double CurrentValX, CurrentValY;
 int ballX,ballY;

Sensor = (SensorConnection) Connector.open("sensor:acceleration");//To open connection
public void run() {
while(true){
 try {
                        data = compass.getData(1);/
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    for (int i = 0; i < data.length - 1; i++) {
                        value[i] = data[i].getDoubleValues()[0];/Get data For X and Y axis 

                    }

                    CurrentValX = value[0];
                    CurrentValY = value[1];

                    if (CurrentValX < PreValueX1) {
                        left = false;
                        right = true;

                    } else if (CurrentValX > PreValueX1) {
                        left = true;
                        right = false;

                    }
                    if (CurrentValY < PreValueY1) {
                        down = false;
                        up = true;

                    } else if (CurrentValY > PreValueY1) {
                        down = true;
                        up = false;

                    }
 if (right == true) {
            ballX += 10;         
        } else if (left == true  {
            ballX -= 10;          
        }
        if (down == true ) {
            ballY += 10;          
        } else if (up == true ) {
            ballY -= 10;            
        }

                    CurrentValY = PreValueY1;
                    CurrentValX = PreValueX1;

dodraw();
repaint();
}

//Function used to draw the image of ball
dodraw(){
ballSprite.setPosition(ballX, ballY);
ballSprite.paint(g);//Graphics==g
}

Now the problem here i am facing is if I decrement the speed eXample :-by writting ballx+=4;ballY+=4 OR ballx-=4;ballY-=4,Then I get the control over my ball

And if my keep my speed as 10 then i don't get control over my ball. In short i can able to get only one thing speed or control.

But i want to do both speed and control.


Solution

  • For a more realistic behaviour, you should store the motion vector of your marble and modify it each frame according to the sensor data and add it to the marble's position every frame. .

    You could go the full simulation way, using the equations for Kinmatics to calculate your marble's speedvector.