PDA

View Full Version : Rocket Physics



Jephz
February 3rd, 2009, 12:29 PM
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

And the bullet code:


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.

rrh
February 3rd, 2009, 12:44 PM
It's a trigonometry thing, right? sin, opposite, hypotenuse, cosine, tangent, etc.
Remember that the AS function Math.sin() etc. takes radians, while the .rotation attribute takes degrees.

neilmmm
February 3rd, 2009, 03:57 PM
have look at this fla i did as a test thing once

it might help .. it was for a paper plane ... not great but shows the point

the code is below


// think xthrust as general thrust
var xthrust:Number ;
// think of ythrust as the angle input
var ythrust:Number ;
// think of drag as your elevators;
var drag:Number;
var planeAngle:Number;
var positionY:Number;
var previousY:Number;
var positionX:Number;
var previousX:Number;
var startX=plane_mc._x;
var startY=plane_mc._y;
var startRotation=plane_mc._rotation;
reset_mc.onRelease=function () {
delete plane_mc.onEnterFrame;
plane_mc._x=startX;
plane_mc._y=startY;
plane_mc._rotation=startRotation;
}

start_mc.onRelease=function () {
xthrust=Number(thrust_txt.text);
ythrust=Number(angle_txt.text);
drag=Number(drag_txt.text);
plane_mc.onEnterFrame = function() {
positionY = plane_mc._y;
positionX = plane_mc._x;
this._x += xthrust;
this._y -= ythrust;
ythrust -= drag;
var planeDY = positionY-previousY;
var planeDX = positionX-previousX;
var radians = Math.atan2(planeDY, planeDX);
this._rotation = radians*180/Math.PI;
previousX = positionX;
previousY = positionY;
//this._rotation;
if (this.hitArea_mc.hitTest(ground_mc)) {
delete plane_mc.onEnterFrame;
this._y=ground_mc._y;
}
};
planeAngle = Math.atan2(positionY-previousY, positionX-previousX);
previousX = positionX;
}

neilmmm
February 3rd, 2009, 04:01 PM
and the fla

Jephz
February 3rd, 2009, 07:05 PM
Thanks man, I'll check it out.