Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3276618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:16:42+00:00 2026-05-17T19:16:42+00:00

I have a sprite animation, a small cannon rendered using a 3D app. I

  • 0

I have a sprite animation, a small cannon rendered using a 3D app. I have exactly 360 frames for a 360 degree turn. Each image has a 100×100 pixel size.

So basically what I am trying todo is when I click anywhere in the page, the barrel of the cannon needs to rotate to point at the mouse cursor, sound simple maybe but I can’t really get it to work very well, perhaps cause my math skills is lacking 😛

What I currently have is something like this

/* This is my div with the cannon background image (360 images stitched into one) each  "image area" is 100x100px */

obj.cannon = $('#cannon');

/* Get the X/Y of the cannon loc in the dom */
var cannonX = $(obj.cannon).offset().left;
var cannonY = $(obj.cannon).offset().top;

/* Get radians using atan2 */
var radians = Math.atan2(e.pageY-cannonY, e.pageX-cannonX);

/* Convert to degrees */
var degrees = radians * (180/Math.PI);

And this is where I am, I mean since the image width is 100px and I need to move the background-position by 100px to move the cannon one degree right, because 360 images * 100px = 36000px in total. So the stitched sprite is like 36000px wide.

So

Insert weird calculation here based on the current backgroundPosition of the image-sprite and apply new backgroundPosition based on where you click with the mouse cursor, then use some sort of setTimeout(animateIt, speed); to “animate” the background position to the new position.

function animateIt(){
  if(newpos!=targetpos) { //Use < > here if we need to add or remove
      newpos+=100; //Until we arrive at the new backgroundPosition
      $(obj.cannon).css({backgroundPosition: newpos+'px' });
      setTimeout(animateIt, speed);
  }
}

Am I at all on the right track here, am I thinking correctly about this? I feel stupid, this should be a simple thing but right now I am having a brain meltdown I think =P. My problem is I don’t know how to properly arrive at the “new target backgroundposition” and then animate it ++ or — based on the current background position :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:16:42+00:00Added an answer on May 17, 2026 at 7:16 pm

    Well, here is a simplified working example with 10 images.

    I’ll post the code and jsFiddle now, and I might come back later to cover it in depth. But basically you just order your images correctly, and then you pick the segment by using (Segments - Math.floor(degree / (360 / segments))). You may have to adjust your 0 degree. For example, I made my 0 equal to what would normal by 90.

    Pay attention to the fact that the screen coordinates, x and y, increase right and down. This makes the degrees of atan work clockwise instead of the usual counter clockwise in coordinate systems where x and y increase right and up.

    I added in some text output that shows the degrees and image segment being shown.

    jQuery handles normalizing the x and y position nicely. Just take care that your CSS setup is cross browser.

    Working jsFiddle example

    Here’s our image:
    alt text

    Here’s our HTML:

    <div id="main"><div id="img"></div></div>
    <div id="info">
        <span></span><br/>
        <span></span>
    </div>
    

    ​
    CSS:

    div#main {
        width:500px;
        height:500px;
        border:2px #000 solid; }
    div#img {
        width:94px;
        height:119px;
        overflow:hidden;
        top:50%;
        left:50%;
        margin-left:-45px;
        margin-top:-60px;
        position:relative; 
        background-image:url('http://imgur.com/3UPki.png');
        background-position:0;}
    div#info {
        position: absolute;
        bottom:0;
        left:0; }
    

    ​

    Javascript / jQuery:

    $(function() {
        var position = $("div#img").position(), 
            mouseX, mouseY, imgX, imgY, degree;
        imgX = position.left;
        imgY = position.top;
        $("#main").mousemove(function(e) {
              // degree is arctan y over x (soh,cah,toa)
            degree = Math.atan2((e.pageY - imgY),(e.pageX - imgX))*(180 / Math.PI);
            degree = (degree - 90) % 360;
              // jQuery normalizes pageX and pageY
              // transfrom from -180 to 180 ==> 0 to 360
            if (degree < 0) degree = 180 + (180 - (Math.abs(degree)));        
            rotate(degree);
            $("span:first").html("Segment: " + (9 - Math.floor(degree / 36)));
            $("span:last").html("Degree: " + Math.floor(degree));          
        }); 
    
        function rotate(degree) {
            var off = 9 - Math.floor(degree / 36);
            $("div#img").css("background-position",-off*94);
        }    
    }); ​
    

    Working jsFiddle example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sprite which i animate using CCAnimate. the animation is composed of
I have an array randomAlphabets which contains CCSprite objects. I need to start animation
I have an array randomAlphabets which contains CCSprite objects. I need to start animation
So i have this sprite that is say arbitrarily 100 x 100 which is
I had an idea for an app that would require me to implement these
What I want to achieve: Drawing png files with alpha exactly as it appears
Okay so I'm working in pure as3 (no creative suite or xml). I have
Kind of a dilemma here. I am making a mobile version of a website,
I am writing a game in which there are thumbnails of mini games displayed
I recently came up with an idea for a simulation/strategy game. I've been sketching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.