Greetings, all! Just need a little help with movement of objects in a more complex fashion - using bearing.
I want to move an object at specific degrees but the following isnt working.
Code:
//Main Class
public void createBullet()
{
Bullet b = new Bullet(getWidth()/2, getHeight()/2, 33, 3);
}
public void run()
{
while (bRunning)
{
try
{
b.move();
b.paint(getGraphics());
Thread.sleep(tickTime);
}
catch (Exception e)
bRunning = false;
}
}
//Bullet Class
//declaration stuff
int x, //Current x co-ord
y, //Current Y co-ord
pX, //Previous x co-ord
pY, //Previous x co-ord
bearing, //Direction in gegrees of movement
speed; //velocity
//some other stuff
//Constructor
public Bullet(int sX, int sY, int sBearing, int sSpeed)
{
//'s' means Start
x = sX;
y = sY;
pX = sX;
pY = sY;
bearing = sBearing;
speed = sSpeed;
}
//move method
public void move()
{
//convert bearing to radians
int angle = bearing * 2 * Math.PI / 360;
//Set previous x & y co-ord
pX = x;
pY = y;
//Use trig functions to get new X an Y co-ords
x += Math.cos(angle) * speed;
y += Math.sin(angle) * speed;
//Mirror the bearing if the x reach the side of the screen
//and all that.
}
//Paint methos
public paint(Graphics g)
{
g.setColor(new Color(0, 255, 0));
g.drawLine(pX, pY, x, y);
}
Ignoring the errors if n e, the code works, however the movement is definately not rite. I hope u get wat im saying, and if n e 1 has n code for the angle movement post it. l8er