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 7048031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:50:33+00:00 2026-05-28T02:50:33+00:00

I have a cannon, a ball, and a trigger. When you press the trigger

  • 0

I have a cannon, a ball, and a trigger. When you press the trigger a linear impulse is applied to the ball.

short int direction = [level.cannon cannon].rotation;

short int power = 24.8;
b2Vec2 force = b2Vec2(direction, power);
[level.ball body]->ApplyLinearImpulse(force, [level.ball body]->GetWorldCenter());

My problem is, when the trigger is pressed, the linear impulse is applied to the ball, but the ball doesn’t actually come out of the top of the cannon sprite.

The reason it is doing this (I think) is because I have set the anchor point, for the cannon, to (0.5, 0).

cannon.anchorPoint = ccp(0.5, 0);

I have figured out that for every 5 degrees the cannon rotates, a multiple of 2 needs to be added/subtracted to the rotation.

E.G.

For 0 to 55 Degrees

0 to 5 subtract 2 from the rotation
5 to 10 subtract 4 from the rotation
10 to 15 subtract 6 from the rotation
etc.

For 0 to -55 Degrees

0 to -5 add 2 to the rotation
-5 to -10 add 4 to the rotation
-10 to -15 add 6 to the rotation

Currently, I am using this code to accomplish this.

if (_cannon.rotation == 0)
    {
        direction = _cannon.rotation;
    } else if (_cannon.rotation >= 1 && _cannon.rotation < 6)
    {
        direction = _cannon.rotation - 2;
    } else if (_cannon.rotation >= 6 && _cannon.rotation < 11)
    {
        direction = _cannon.rotation - 4;
    } else if (_cannon.rotation >= 11 && _cannon.rotation < 16)
    {
        direction = _cannon.rotation - 6;
    } else if (_cannon.rotation >= 16 && _cannon.rotation < 21)
    {
        direction = _cannon.rotation - 8;
    } else if (_cannon.rotation >= 21 && _cannon.rotation < 26)
    {
        direction = _cannon.rotation - 10;
    } else if (_cannon.rotation >= 26 && _cannon.rotation < 31)
    {
        direction = _cannon.rotation - 12;
    } else if (_cannon.rotation >= 31 && _cannon.rotation < 36)
    {
        direction = _cannon.rotation - 14;
    } else if (_cannon.rotation >= 36 && _cannon.rotation < 41)
    {
        direction = _cannon.rotation - 16;
    } else if (_cannon.rotation >= 41 && _cannon.rotation < 46)
    {
        direction = _cannon.rotation - 18;
    } else if (_cannon.rotation >= 46 && _cannon.rotation < 55)
    {
        direction = _cannon.rotation - 20;
    } else if (_cannon.rotation <= -1 && _cannon.rotation > -6)
    {
        direction = _cannon.rotation + 2;
    } else if (_cannon.rotation <= -6 && _cannon.rotation > -11)
    {
        direction = _cannon.rotation + 4;
    } else if (_cannon.rotation <= -11 && _cannon.rotation > -16)
    {
        direction = _cannon.rotation + 6;
    } else if (_cannon.rotation <= -16 && _cannon.rotation > -21)
    {
        direction = _cannon.rotation + 8;
    } else if (_cannon.rotation <= -21 && _cannon.rotation > -26)
    {
        direction = _cannon.rotation + 10;
    } else if (_cannon.rotation <= -26 && _cannon.rotation > -31)
    {
        direction = _cannon.rotation + 12;
    } else if (_cannon.rotation <= -31 && _cannon.rotation > -36)
    {
        direction = _cannon.rotation + 14;
    } else if (_cannon.rotation <= -36 && _cannon.rotation > -41)
    {
        direction = _cannon.rotation + 16;
    } else if (_cannon.rotation <= -41 && _cannon.rotation > -46)
    {
        direction = _cannon.rotation + 18;
    } else if (_cannon.rotation <= -46 && _cannon.rotation > -55)
    {
        direction = _cannon.rotation + 20;
    }

I know there has to be an easier way to do this, but I’m still learning. Can someone please help me?

  • 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-28T02:50:34+00:00Added an answer on May 28, 2026 at 2:50 am
     short int val, val2;
     val = _cannon.rotation/5;
     val2 = _cannon.rotation%5; //remainder of the division
                                //it will be zero when rotation is a multiple of 5. 
     if(val2 > 0)
        val += 1;
     direction = _cannon.rotation - 2*val;
    

    I’m not sure if this is what you want, and I didn’t test it. Just made some mental tests to see if it works for the cases you put.

    But I hope you can use this as a starting point for making your code smaller.

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

Sidebar

Related Questions

I have an XML File of this sort <BallList> <Brand name=xyz> <BallName>ball A</BallName> <DateApproved>Jan-12</DateApproved>
I have a class called Ball, and i want to call a method called
I have game application (cocos2d) in which, a ball is to be shooted from
I have two queries that I have to run, I cannon join them But
Assuming I have a table like this one: CREATE TABLE user_delegates ( [id] INT
I have a ball and another sprite. When the ball collides with the sprite
I have two images moving across the screen, one is a ball and one
I have a sprite animation, a small cannon rendered using a 3D app. I
I have an XML file filename.xml with the contents <product> <sku>144249</sku> <productId>119223</productId> <name>Cannon Digital
Out of the box SSRS reports cannot have data exposed in the page header.

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.