Aim N Shoot Question

edited February 2013 in Development
Hey, i am creating game and I need some help.
I know how to make script that will return x and y coordinates of the mouse/touchpad, but I dont know how to return weapon angle around player to crosshair.

Comments

  • I think what you are look for is Math.atan2(y,x) where x and y have (0,0) on the player center.
  • edited February 2013
    Thank you but.... The script should somehow compare two positions to return angle, not one.
    And angle should be returned in degrees too
  • Accepted Answer
    Let's say that the player position on screen is (x0,y0) and that the mouse click was on (x1,y1). The formula to find the angle is:

    angle = Math.atan2(x1-x0, y1-y0);

    You just have to keep in mind that the y-axis point toward the bottom and the origine (0,0) is on the top-left corner of the window.
  • edited February 2013
    "
    function RadToDeg(radians){
    //converting radians to degrees
    degrees = radians*(180/Math.PI);
    return degrees;
    }

    function ReturnPlayerAimDegree(PlayerX,PlayerY,event){
    //Everything's clear lets go
    var PlayerCurrentMouseX = event.clientX;
    var PlayerCurrentMouseY = event.clientY;
    this.TransX = PlayerCurrentMouseX-PlayerX;
    this.TransY = PlayerCurrentMouseY-PlayerY;
    var RadDegPlayerOperation = Math.atan2(this.TransX,this.TransY);
    document.getElementById("demo").innerHTML = RadToDeg(RadDegPlayerOperation);
    return RadToDeg(RadDegPlayerOperation);
    }

    "
    This script above works well
    Thank you,
    Everything's clear
    Hope I will make aiming script correctly
  • OK, I've got it EXCEPT 1 thing I have to say:
    Mx = event.clientX;
    My = event.clientY;
    // x, y are pre-defined as player x and y s, offsets are predefined by the angle I aim and, gun size, etc.
    transX = x - Mx;
    transY = y - My
    var GunAimRadians = math.atan2(transX - Xoffset, transY - Yoffset)
    var Aim = GunAimRadians*180/Math.PI - 90. //-90 is VERY important or otherwise it will aim 90 deg up
    Then.... bullets, etc.
    You know, I have been experimenting with "Tululoo Game Maker"(uses pure JS, no frameworks such as JQuery) and I have found something interesting here:
    http://trickkr.com/item/67/rezoners-games-from-scratch-bullets

  • It also has answered me a BIG question for direction of bullets and speed relation. Again, trigonometry.
Sign In or Register to comment.