# Rocket Physics

**URL:** <https://forum.kirupa.com/t/rocket-physics/280972>\
**Category:** programming\
**Created:** [February 3, 2009, 4:29pm UTC](https://forum.kirupa.com/t/rocket-physics/280972 "2009-02-03T16:29:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jephz](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jephz](https://forum.kirupa.com/u/Jephz)\
**Post date:** [February 3, 2009, 4:29pm UTC](https://forum.kirupa.com/t/rocket-physics/280972/1 "2009-02-03T16:29:41Z")

</div>

Hey, guys me and a friend are working on a artillery game. No epic, awsomeness, just a simple game for practice. We are currently up to the bullet/rocket firing part, and we are experiencing a problem. See, usually bullets are round when fired, so no rotation is required. But we are using rockets, long ones instead of round bullets so rotation IS required. We don’t know how to code this, and no tutorial has ever been made for this kind.

Here is our current progress of the game:

[http://spamtheweb.com/ul/upload/010209/74736\_artillery\_game\_4.php](http://spamtheweb.com/ul/upload/010209/74736_artillery_game_4.php)

And the bullet code:

```auto
Mouse.hide();
gravity = 4;
speed = 5;
i = 1;
mining = true;
minesvar = 3;
minestotal = 3;
ammovar = 50;
ammototal = 50;
armourvar = 50;
armourtotal = 50;
moneyvar = 0;
mineplace = true;
firing = true;
attachMovie("crosshair","crosshair",1);
attachMovie("tank","tank",2,{_x:230, _y:410});
_root.attachAudio("fire","fire");
fire2 = new Sound();
fire2.attachSound("fire");
explode2 = new Sound();
explode2.attachSound("explode");
crosshair.onEnterFrame = function() {
this._x = _xmouse;
this._y = _ymouse;
};
tank.onEnterFrame = function() {
minex = this._x-20;
miney = this._y+10;
mousex = _xmouse-this._x;
mousey = (_ymouse-this._y)*-1;
angle = Math.atan(mousey/mousex)/(Math.PI/180);
if (mousex<0) {
angle += 180;
}
if (mousex>=0 && mousey<0) {
angle += 360;
}
if (angle>160) {
angle = 160;
}
if (angle<20) {
angle = 20;
}
firepower = Math.sqrt(mousex*mousex+mousey*mousey);
if (firepower>200) {
firepower = 200;
}
this.cannon._rotation = angle*-1;
if (Key.isDown(65)) {
this._x -= speed;
_root.bg._x -= (speed/2);
_root.vcam.dot._x -= (speed/7);
moving = true;
} else {
moving = false;
}
if (Key.isDown(68)) {
this._x += speed;
_root.bg._x += (speed/2);
_root.vcam.dot._x += (speed/7);
moving = true;
} else {
moving = false;
}
if (Key.isDown(Key.SPACE)) {
if (mineplace && mining) {
attachMovie("mine","mine2"+k,_root.getNextHighestD epth(),{_x:minex, _y:miney});
minesvar -= 1;
mining = false;
}
}
if (!Key.isDown(Key.SPACE)) {
if (!mining) {
mining = true;
}
}
};
function onMouseDown() {
if (firing && !moving) {
angle = tank.cannon._rotation-1;
start_ball_x = tank._x+48*Math.cos(angle*Math.PI/180);
start_ball_y = tank._y+48*Math.sin(angle*Math.PI/180);
cannonball_fired = attachMovie("cannonball", "cannonball_"+i, _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
i++;
ammovar -= 1;
_root.fire2.start(0,0);
cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*firepower;
cannonball_fired.diry = Math.sin(angle*Math.PI/180)*firepower;
cannonball_fired.onEnterFrame = function() {
this.diry += gravity;
this._x += this.dirx/50;
this._y += this.diry/50;
};
}
}
onEnterFrame = function () {
  for (j=0; j<i; j++) {
if (_root.ground.hitTest(_root["cannonball_"+(i-1)])) {
_root.explode2.start(0,0);
_root["cannonball_"+(i-1)].removeMovieClip();
}
}
if (_root.tank.hitTest(_root.wall)) {
_root.tank._x += speed;
_root.bg._x += (speed/2);
_root.vcam.dot._x += (speed/7);
}
if (_root.tank.hitTest(_root.wall2)) {
_root.tank._x -= speed;
_root.bg._x -= (speed/2);
_root.vcam.dot._x -= (speed/7);
}
_root.vcam.armour = Math.round((armourvar/armourtotal)*100)+"%";
_root.vcam.ammo = ammovar+"/"+ammototal;
_root.vcam.mines = minesvar+"/"+minestotal;
_root.vcam.money = moneyvar;
if (minesvar == 0) {
mineplace = false;
}
if (ammovar == 0) {
firing = false;
}
};

```

The code not the fully functional code, and still has some problems. I’m doing the art (So far, I’ve only got up to the tank) so please excuse the bad art for now.

Code by Andy70707 of Newgrounds.
