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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:45:06+00:00 2026-06-02T05:45:06+00:00

I am encountering an issue when updating a bullet’s position when I rotate the

  • 0

I am encountering an issue when updating a bullet’s position when I rotate the ship that shoots it. Currently the code almost works except that at most angles the bullet is not fired from where i want it to. Sometimes it fires slightly upward than the centre where it should be, and sometimes downwards. I’m pretty sure it has something to do with the trigonometry but I’m having a hard time finding the problem.

This code is in the constructor; it sets the bullet’s initial position and rotation based on the ship’s position (sp.position) and its rotation (Controls.rotation):

this.backgroundRect = new RotatedRectangle(Rectangle.Empty, Controls.rotation);
//get centre of ship
this.position = new Vector2(sp.getPosition().X + sp.getTexture().Width/2, 
(sp.getPosition().Y + sp.getTexture().Height / 2));

//get blast pos
this.position = new Vector2((float)(this.position.X + (sp.getTexture().Width / 2 * 
Math.Cos(this.backgroundRect.Rotation))), (float)(this.position.Y + 
(sp.getTexture().Width / 2 * Math.Sin(this.backgroundRect.Rotation))));

//get blast pos + texture height / 2
this.position = new Vector2((float)(this.position.X + (this.texture.Height / 2 * 
Math.Cos(this.backgroundRect.Rotation))), (float)(this.position.Y + 
(this.texture.Height / 2 * Math.Sin(this.backgroundRect.Rotation))));
        
this.backgroundRect = new RotatedRectangle(new Rectangle((int)this.position.X, 
(int)this.position.Y, this.texture.Width, this.texture.Height), Controls.rotation);
speed = defSpeed;

Then in the update method I use the following code; I’m pretty sure this is working fine though:

this.position.X = this.position.X + defSpeed * 
(float)Math.Cos(this.getRectangle().Rotation);
this.position.Y = this.position.Y + defSpeed * 
(float)Math.Sin(this.getRectangle().Rotation);
this.getRectangle().CollisionRectangle.X = (int)(position.X + defSpeed * 
(float)Math.Cos(this.getRectangle().Rotation));
this.getRectangle().CollisionRectangle.Y = (int)(position.Y + defSpeed * 
(float)Math.Sin(this.getRectangle().Rotation));
    

Also I should mention that my ship had not been working correctly when I rotated it since the origin (center of rotation) was (0,0). To remedy this I changed the origin to the center of the ship (width/2,height/2) and also added those values to the drawing rectangle so that the sprite would draw correctly. I imagine this could be the problem and suppose there’s a better way of going around this. The game is in 2D by the way.

  • 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-06-02T05:45:08+00:00Added an answer on June 2, 2026 at 5:45 am

    The problem is that the sprites are not drawn on the positions they are supposed to be. When calculating the center position, you assume that the sprite is not rotated. This causes some trouble. Furthermore, rotating an arbitrary vector is not as easy as you did. You can only rotate a vector by multiplying its components with sin/cos when it is on the x-axis.

    Here is how you can rotate arbitrary vectors:

    Vector2 vecRotate(Vector2 vec, double phi)
    {
        double length = vec.Length(); //Save length of vector
        vec.Normalize();
        double alpha = Math.Acos(vec.X); //Angle of vector against x-axis
        if(vec.Y < 0)
            alpha = 2 * Math.PI - alpha
        alpha += phi;
        vec.X = Math.Cos(alpha) * length;
        vec.Y = Math.Sin(alpha) * length;
        return vec;
    }
    

    Make sure, the angle is in radians.
    With that you can calculate the correct position of the ship:

    Vector2 ShipCenter = sp.Position() + vecRotate(new Vector2(sp.getTexture().Width/2, sp.getTexture().Height/2), Controls.Rotation);
    

    You can use the same function to determine the position of the bullet’s center. I don’t exactly know, where you want to place the bullet. I will assume, it is at the center of the right edge:

    Vector2 offset = new Vector2(sp.getTexture.Width/2, 0);
    this.position = ShipCenter + vecRotate(offset, Controls.Rotation);
    

    If you then need the position of the upper left corner of the bullet, you can go further:

    this.position -= vecRotate(new Vector2(this.texture.Width/2, this.texture.Height/2), Controls.Rotation)
    

    However, as I stated, it is probably easier to handle the ship’s and bullets’ positions as central positions. Doing so, you need far less trigonometry.

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

Sidebar

Related Questions

I've been messing around with a radiobox. I'm encountering an issue that varies depending
I'm encountering an interesting issue with the following code. productObject is a custom object
How are error productions typically added? I'm encountering the issue that my error productions
I'm encountering a weird issue in Java at the moment that I've never seen
I'm encountering an annoying shape mismatch issue when I'm working with arrays that are
I am encountering an issue where having a ending script tag inside a quoted
Probably the following description of the issue I'm encountering will be a bit vague,
The problem I am encountering is that for my login form I have to
I have been encountering an issue with inserts in EF 4.0. During a migration
I'm working with a Droid / Android 2.0.1 and encountering an issue apparently many

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.