making projectiles travel at a fixed velocity

Started by
1 comment, last by tmccolgan88 10 years, 9 months ago

I'm having trouble making projectiles travel at the same speed. Right now I'm generating a bullet at a fixed point toward the bottom of the screen in response to a TouchEvent. Using the coordinates of the touch and starting position of the bullet I'm calculating a vector to make the bullet travel on the correct path. Currently the accuracy is dead on, but depending on the location of the touch the bullet will travel faster or slower. Right now I'm calculating what percent the touchEvent's y value is of a fixed value, and then multiplying the vector's x value by that number. It works pretty well except for when the vector's y value gets close to zero. Wondering if someone can see what I'm doing wrong, or steer me in the direction of the proper technique.

This is the code that populates the bullet object;


if (touchEvents.size() > 0) {
			for (int i = 0; i < touchEvents.size(); i++) {

				turretTickTimer += deltaTime;

				storeXBullet = touchEvents.get(i).x;
				storeYBullet = touchEvents.get(i).y;
				inputCode = touchEvents.get(i).type;

				if (touchEvents.get(i).y < 950
						&& turretTickTimer >= TURRET_TIMER) {
					bullets.add(new Bullet(Assets.bullet,
							((storeXBullet) - TURRET_X_POSITION),
							((TURRET_Y_POSITION - storeYBullet))));
					turretTickTimer = 0;
				}
			}
		} else {
			turretTickTimer += deltaTime;
			if (turretTickTimer >= TURRET_TIMER && inputCode != 1
					&& storeYBullet < 950) {
				bullets.add(new Bullet(Assets.bullet,
						((storeXBullet) - TURRET_X_POSITION),
						((TURRET_Y_POSITION - storeYBullet))));
				turretTickTimer = 0;
			}
		}

This is the bullet class.


package com.turretgame.sprites;

import java.util.Vector;

import android.graphics.Rect;
import android.util.Log;

import com.example.gamedevchapter3.Graphics;
import com.example.gamedevchapter3.Pixmap;
import com.turretgame.mainpackage.Assets;

public class Bullet {
	
	final int TURRET_Y_POSITION = 1040 + (Assets.turret.getHeight() / 2);
	final int TURRET_X_POSITION = 360 + (Assets.turret.getWidth() / 2);

	final float VELOCITY = 1f;
	
	Pixmap pixmap;
	public Rect intersectRect;
	
	float xpos, ypos;
	float velocityAdjustment = 0;
	float movementVectorX, movementVectorY = 0;
	
	public Bullet(Pixmap pixmap, float moveX, float moveY){
		this.pixmap = pixmap;
		
		velocityAdjustment = (float)(10.0f / moveY);
		
		this.movementVectorX = moveX * velocityAdjustment;
		this.movementVectorY = 10;//moveY;
		
		xpos = TURRET_X_POSITION;
		ypos = TURRET_Y_POSITION;
		
		intersectRect = new Rect((int)xpos, (int)ypos, (int)xpos + 5, (int)ypos+ 5);
	
	}
	
	public void update(){
		xpos += (movementVectorX * VELOCITY);
		ypos -= (movementVectorY * VELOCITY);
		
		intersectRect.set((int)xpos, (int)ypos, (int)xpos + 5, (int)ypos+ 5);
	}
	
	public void draw(Graphics g){
		g.drawPixmap(pixmap, (int)xpos, (int)ypos);
		
	}
	
	public float getY(){
		return ypos;
	}
	
	public float getX(){
		return xpos;
	}

}

Any help is appreciated.

Advertisement

When you create a bullet, you bring in a vector that goes from the turret to the touch location (moveX & moveY). In order to have a constant velocity in this direction, this must be normalized.


//in bullet constructor before using moveX & moveY
float magnitude = Math.Sqrt((moveX * moveX) + (moveY * moveY));
 
this.movementVectorX = moveX / magnitude;
this.movementVectorY = moveY / magnitude;

now the screen distance covered by updating the bullet will be constant no matter where the touch event occurred.

Also there will be no need for the velocity adjustment in the constructor as well.

Thanks Shazen, works perfectly now.

This topic is closed to new replies.

Advertisement