Hey ya’ll! This is my first post here as I am new to processing in general. I need to make a game for a project I am working on, in which a gun mounted In a fixed position shoots projectiles at incoming targets. I need to find a way to make the bullets start their trajectory at this fixed points and then travel to where I clicked using vectors somehow. I cannot find any information specifically pertaining to what I need, so I decided to come here.
It’s simple maths
Say your gun is at point gx, gy and mouse is at
mx,my.
What do you have to add to the former to go to the latter? Can you split this up into smaller parts so
you can animate the bullet?
xSpeed = mouseX-x;
ySpeed = mouseY-y;
xSpeed *= .03;
ySpeed *= .03;
It’s policy here not to give code solutions.
So please show your code.
Hey and welcome to the forum!
Great to have you here!
Chrisir
Hey Chris! Here is the code I have so far. I need the bullet to go where I click, however I do not know what sort of command/math (I am rusty at math) to use. I don’t need any code I just need a push in the right direction.
ArrayList ballList;
void setup(){
size(800,600);
background(0);
ballList = new ArrayList();
}
void draw(){
background(45);
for (Ball aBall: ballList){
aBall.render();
aBall.move();
}
rect(600,400,100,20);
}
void mousePressed(){
ballList.add(new Ball());
}
class Ball {
int x;
int y;
int d;
color aColor;
int xSpeed;
int ySpeed;
/*
Constructor
*/
Ball(int tempX, int tempY, int tempD, color tempColor) {
x = tempX;
y = tempY;
d = tempD;
aColor = tempColor;
// xSpeed = int(random(-10, 10));
ySpeed = int(random( 10));
if (xSpeed == 0) {
xSpeed =10;
}
if (ySpeed == 0) {
ySpeed = 10;
}
}
Ball() {
x = int(650);
y = 400;
d = int((5));
aColor = color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
xSpeed = int(( 10));
ySpeed = int((10));
if (xSpeed == 0) {
xSpeed =10;
}
if (ySpeed == 0) {
ySpeed = 10;
}
}
/*
Functions
*/
void render() {
fill(250,20,50);
circle(x, y, d);
}
/*
This function takes updates the position the ball according to its speed.
*/
void move() {
y -= ySpeed;
}
/*
This function checks if the ball is touching an edge. If it is, the speed flips
*/
void wallDetect() {
if (y+d/2 >= height) {
ySpeed = abs(ySpeed);
}
if (y-d/2 <= 0) {
y=height;
}
}
}
The box is a stand in for the gun that I have drawn already
That’s what I described above. Can you make it happen?
You can get the mouse position using mouseX, mouseY
Thanks mate, I will give it a shot!