School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,455 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,600 people online right now. Registration is fast and FREE... Join Now!




Something wrong with my trigonometry...

 

Something wrong with my trigonometry...

ikofthetoids

7 Jun, 2009 - 06:29 PM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 30



Thanked: 1 times
My Contributions
I'm making a game where you operate a cannon turret and shoot at yet-undetermined enemies. Here's all the code I have:

CODE
this.stop();
//variables
var BBs:Array=new Array();
var uniVar:Object=new Object();
//event listeners
stage.addEventListener(MouseEvent.CLICK, addBullet);
stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:Event) {
    canon_1.rotation=(180/Math.PI)*Math.atan((275-mouseX)/mouseY);
});
                                                              
//Game Loop is called every 10 miliseconds
var interval=setInterval(function() {
    if (BBs.length>0) {
        monitorBullets();
    }
}, 10);
//adds a bullet
function addBullet(e:Event):void {
    var cBul:Bullet=new Bullet();
    var dir:Number=-(canon_1.rotation/90);
    cBul.x=275+100*Math.cos((180/Math.PI)*(-canon_1.rotation+270));
    cBul.y=100*Math.sin((180/Math.PI)*(-canon_1.rotation+270));
    addChild(cBul);
    BBs.push([cBul, dir]);
}
//handles the bullets
function monitorBullets():void {
    uniVar.todo=false;
    BBs.forEach(function(item:*, index:int, arr:Array) {
        BBs[index][0].x+=3*BBs[index][1];
        BBs[index][0].y+=3*(1-Math.abs(BBs[index][1]));
        if (BBs[index][0].x>570 || BBs[index][0].x<-20 || BBs[index][0].y>420) {
            uniVar.todo=true;
            uniVar.numb=index;
        }
    });
    if (uniVar.todo) {
        removeChild(BBs[uniVar.numb][0]);
        BBs.splice(uniVar.numb, 1);
    }
}

Everything works (there's no actionscript errors), but it doesn't behave the way I want it to. The cannon rotates fine (by the way, it's located top-center of the stage) and always points at the mouse, and the bullets have the right direction and speed, but they're getting placed weird. It's at lines 21 and 22 (located in the function "addBullet") that places the bullets. I'm trying to have them start at the tip of the cannon (like normal bullets would) and shoot off screen, and seeing as I have the polar coordinates of the tip of the cannon, I tried to convert it to cartesian coordinates, but it's not working. sometimes the bullets start out of the screen and shoot down. Sometimes they start on the other side of the cannon. But they always have the right direction smile.gif

Does anybody know how I could get the cartesian coordinates of a polar point where r=100 and theta is variable?

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: Something Wrong With My Trigonometry...

7 Jun, 2009 - 09:04 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
Polar to Cartesian conversion is like so (Assuming I can remember the trig):

CODE

x = r*Math.cos(theta);
y = r*Math.sin(theta);


At least according to calc.

HTH
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:59AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month