Need help with code. (cannon)

edited January 2017 in Questions about Code

I have somehow managed to get this game started..but the angle of aim does not match the angle of the gun. Can someone please fix the codes? I really don't have time...i need to get it done by midnight..will someone please help me with my project.

float cannonX, cannonY; float cannonLength = 40; // How long the cannon on the tank extends float cannonAngle = PI*3/4; float cannonAngleStep = 0.005; // step to change cannonAngle float cannonPower = 0;

float gravity = 0; // Strength of gravity float gravitySpeed = 0.00981; float time; // calculate time for the gravity component float wind = 0; // Strength of wind float explodePositionX; float explodePositionY; boolean explode = false; float explosionStrength = 1;

float bulletSpeedX = 0; float bulletSpeedY = 0; float bulletX = 0; float bulletY = 0; Boolean renderBullet = false;

float chargeAngle = PI*3/4.; Boolean charging = false;

boolean objectInMotion = false; float targetDistance = 50; // distance from left boundary the target is positioned boolean targetHit = false; int score = 0; int total = 0;

float[] groundLevel; // Y coordinate of the ground for each X coordinate float x,y; // position of the projectile float r = 10; // radius of the projectile float speed =20.0; // speed of projectile

void setup (){ size (1000,600); cannonX = width-20; cannonY = height/2;

// Initialize the ground level // Implement random surface of the ground groundLevel = new float[width]; for(float i = 0; i < width; i++) { groundLevel[(int)i] = height/3; }

// Load font

}

void draw(){ background(200); drawGround(); drawTarget(); drawCannon(); aimCannon(); shootCannon(); drawProjectile(); drawScore(); }

void drawGround(){ // See the groundLevel[] variable to know where to draw the ground // Ground should be drawn in a dark gray stroke(118,57,40); // set dark brown for(int x = 0; x < width; x++) { line (x,height, x,height-groundLevel[x]); // draw in loop a vertikal line at lokation x } }

void drawTarget(){ ellipseMode(RADIUS); for (int i = 10; i >0; i--){ if (i<4) { fill(0); stroke(255); } else{ fill(255); stroke(0); } ellipse(targetDistance,height/2, 10i, height/2/11i);

} stroke(2555,0,0); line(targetDistance,height/11/2, targetDistance,height-height/11/2); }

void drawCannon(){ stroke(85,107,47); // set darkolivegreen color to draw the cannon fill(85,107,47); smooth(); strokeWeight(8); // invert the sign of the angle in order to point the cannon upwards (and not downwards) line(cannonX, cannonY, cannonX+cannonLengthcos(-cannonAngle), cannonY+cannonLengthsin(-cannonAngle)); strokeWeight(1); // reset to default widht arc(cannonX, cannonY, cannonLength*3/4, cannonLength/2, -PI-PI/6, PI/6); }

void aimCannon(){ fill(100); ellipse(mouseX, mouseY,1, 1); }

void shootCannon(){ if(mousePressed){ charging = true;

//increase power cannonPower++;

//draw power charge strokeWeight(2); stroke(255,0,0); line(cannonX,cannonY,cannonX + cos(cannonAngle - chargeAngle) * cannonPower,cannonY + sin(cannonAngle-chargeAngle)cannonPower); line(cannonX,cannonY,cannonX + cos(cannonAngle + chargeAngle) * cannonPower,cannonY + sin(cannonAngle+chargeAngle)cannonPower); // invert the sign of the angle in order to point the cannon upwards (and not downwards) }else{ if(charging){

//find the correct speed from angle & power bulletSpeedX = cos(cannonAngle) * cannonPower; bulletSpeedY = sin(cannonAngle) * cannonPower;

//set the start position of the bullet at the position of the user bulletX = cannonX; bulletY = cannonY;

//tell the program to render the bullet renderBullet = true;

charging = false; cannonPower = 0; } }

if(renderBullet){ //apply gravity gravity += gravity; bulletSpeedY += gravity; //apply speed bulletX += bulletSpeedX; bulletY += bulletSpeedY;

//render the bullet fill(255,0,0); ellipse(bulletX,bulletY,5,5);

//check if we still need to render the bullet if(bulletY > 600) renderBullet = false; }

}

void drawProjectile(){ // Implementation // drawExplosion(); }

void drawScore(){ // Display score // Implementation }

// Draw esplosion, if collision with target is detected void drawExplosion() { if(explode){ noStroke(); fill(255, explosionStrength*2, 0); ellipse(explodePositionX, explodePositionY, explosionStrength, explosionStrength); explosionStrength+=5; // delay(2); if (explosionStrength>100){ explode=false; explosionStrength=1; } } }

Tagged:

Answers

  • Where did you get this code from? If this is homework, you should be doing it yourself. I mean, you are not learning anything if you ask for this type of questions in the forum.

    One thing, in your function drawTarget(), remove ellipseMode(RADIUS);

    To get things going, you need to update your cannonAngle in your function aimCannon(). Check for atan2 in the the reference. Search for previous post with the keyword asteroid as it will show you relevant examples.

    Kf

  • please don't post duplicates.

  • So there are two things you need to do:

    Change1: bulletSpeedY = sin(cannonAngle) * cannonPower;
    for bulletSpeedY = -sin(cannonAngle) * cannonPower;

    Change2:

    void aimCannon() {
      fill(100);
      ellipse(mouseX, mouseY, 1, 1);
      cannonAngle=-atan2(mouseY-cannonY,mouseX-cannonX);
    }
    

    Kf

  • you don't use gravitySpeed.

    gravity += gravity; looks wrong. that's doubling gravity every time?

Sign In or Register to comment.