Greetings!

I'm almost finished in creating a geometry wars like game (except that you cant move =P ). For the last few days I've been stuck doing the diagonal movement of the bullets. I wish someone could help me =D

here's the constructor of the bullet class
x & y is the starting position of the bullet, while destinationX & Y is where the bullet should go.

public Bullet(int x, int y, int destinationX, int destinationY, GameEngine Engine) {
    	super(x,y,Engine);
    	setImage("graphics/bullet.png");
    	destX = destinationX;
    	destY = destinationY;
    	curX = x;
    	curY = y;
        calculate();
    }
private void calculate () {
    	if ( curX - destX > 0 ) {
    		destX = curX - destX;
    		destX = -destX;
    	}
    	else {
    		destX = destX - curX;
    	}
    		
    	if ( curY - destY > 0 ) {
    		destY = curY - destY;
    		destY = -destY;
    	}
    	else {
    		destY = destY - curY;
    	}
}

if the object is outside of screen, the object no longer exists

public void update() {
    	move();
    	if (x < 0 || x > 1920 || y < 0 || y > 1200)
    		exist = false;
    }

useless move method

public void move() {
    	x += destX;
    	y += destY;
    }

I'm having a hard time figuring out the movement.
the player is on the center of the screen. And mouse clicks determine where the bullet will be shot at.

my main problem is the move method it has to move the bullet up to the end of the screen and it has to move it in a fixed speed.

for example:
The player is at (70,60) and I clicked my mouse at (11,3), not only do the bullet need to move to (11,3), it has to move in a fixed speed (e.g. 3 pixels per frame) and it has to move toward the end of the screen (e.g. (600,0) ) ( not just to the point where the mouse is clicked (11,3) ).

The vertical & horizontal movement is not difficult but the diagonal movement is driving me crazy :'(

any help is greatly appreciated =D

Sounds like a job for vector math! What you want to do is figure out the unit vector that points in that direction. Then multiply that unit vector by the speed you want it to go.

Here's the way I would do it. First of all, store bullet positions as doubles. Also, give the bullet a 'deltaX' and 'deltaY' parameter to represent the velocity vector.

public Bullet(int x, int y, int destinationX, int destinationY, GameEngine Engine) {
    	super(x,y,Engine);
    	setImage("graphics/bullet.png");
    	curX = x;
    	curY = y;
        deltaX = destinationX - x;
        deltaY = destinationY - y;
        
        double mag = sqrt( deltaX*deltaX + deltaY*deltaY );

        deltaX = deltaX / mag * BULLET_SPEED;
        deltaY = deltaY / mag * BULLET_SPEED;
    }

From there, just add deltaX and deltaY to curX and curY every step of the game, and it should work out just fine. It probably goes without saying that you need doubles here so that truncation doesn't result in the bullet flying in a straight line.

Hopefully this will get your code on the right track. Good luck to you.

Comments
Nice post.
Good man, saved my programing ass =P

THANK YOU!!!! (jumping up and down =D) I'm so happy right now, thanks a lot, I'll get to work right away.

This article has been dead for over six months. Start a new discussion instead.