XNA Creators Club Online
Page 1 of 1 (22 items)
Sort Posts: Previous Next

2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

Last post 24/05/2009 2:30 by Cypher1. 21 replies.
  • 15/01/2009 2:55

    2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Well, I am extremely new to programming and everything having to do with it, and I am really interested in it, so I figured what better way to learn, than to use an interactive tutorial. So far, it have been going great, and I have been learning some of the fundamentals of programming with C#, and I just got to the part of the tutorial, where we have just drawn the cannonBall sprites onto the screen.  The problem is that the cannon balls do not originate from the barrel of the cannon, like I would like them to, but they seem to originate from the base of the cannon, which is not pleasing to the eye, and, when the cannon is moving and shooting, it can effect the coordination of my shots. So I'm asking if anybody has any solutions for this problem, and if so, if they can walk me through the solution.
  • 15/01/2009 6:45 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Ahhh yes, I remember this problem. The solution is quite simple, here is my updated code...
            public void FireCannonBall(float deltaRotation)  
            {  
                foreach (GameObject ball in cannonBalls)  
                {  
                    if (!ball.alive)  
                    {  
                        Vector2 cannonDirection = new Vector2((float)Math.Cos(cannon.rotation), (float)Math.Sin(cannon.rotation)); //cannon direction  
                        Vector2 ballDirection = new Vector2((float)Math.Cos(cannon.rotation + deltaRotation), (float)Math.Sin(cannon.rotation + deltaRotation)); //cannon direction + added rotation  
                          
                        float barrelLength = cannon.sprite.Width / 2.0f; //look at the cannon texture to see why!  
                        Vector2 barrelPosition = cannonDirection * barrelLength; //vector math  
     
                        ball.alive = true;  
                        ball.position = cannon.position + barrelPosition - ball.center; //start at the end of the cannon  
                        ball.velocity = ballDirection * 15.0f; ; //direction of ball with speed mult. of 15  
     
                        return;  
                    }  
                }  
            } 
    You would need "Vector2 cannonDirection = stuff" and also "ball.position = other stuff" to get the adjustments you want.

    You will also notice I added an argument, deltaRotation.

    To fire one ball in the cannon's direction use...
    FireCannonBall(0.0f); 

    I like to create 3 shots instead...
    FireCannonBall(0.0f);  
    FireCannonBall(MathHelper.PiOver4 / 2);  
    FireCannonBall(-MathHelper.PiOver4 / 2); 
  • 15/01/2009 23:52 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Thank you so much... well i havn't done it yet because I just found your reply, but i'm about to do it. I am however, a bit confused when you said
    "To fire one ball in the cannon's direction... I like to creat 3 shots instead"
    I'm guessing that this means the number of balls that come out of the cannon.  If not, please clarify this for me.  Seriously though, thanks a lot for the help, I really appreciate it.
    Also just wondering... are really good at this, because I don't think I could have figured out how to fix this problem.
  • 16/01/2009 0:02 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    I'm having a slight problem.  I added the code you said (the big chunk anyway, because I didn't know where to put the other stuff) and I have two errors showing this:
    No overload for method 'FireCannonBall' takes '0' arguments 
    I'm sure that I'm just making a stupid mistake, but if you could help me out again, that'd be great.
  • 16/01/2009 0:38 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Ok sorry about all these continuous updates, but I fixed the ball not coming out of the barrel problem. In the tutorial section where we shoot the cannon balls, they have a little not that shows how to fix that. When standing still and firing it's fine, but when i'm moving the barrel and firing, it looks like the ball is swerving away from the barrel once i start moving.  I wish that the ball would actually originate from the tip of the barrel. I think that that is what you were trying to show me in you code, but it got me confused, so if you could clear things up a bit... that would be great.
  • 16/01/2009 0:53 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    First things first, in my updated FireCannonBall function, you must send a rotation value when the shot button is pressed. I called this deltaRotation to represent the change in angle the ball will travel from the barrel. In your code when you check if the A button was pressed, you call the function to shoot a ball when you say FireCannonBall(); This needs to be changed to FireCannonBall(0.0f); This is basically saying "fire the cannon ball in the cannon's direction". If you call the function using FireCannonBall(MathHelper.PiOver4) it is basically saying "fire the cannon ball in the cannon's direction and add PI/4 to that direction."

    EDIT: When I create three shots, I call my FireCannonBall function three times, each with a different deltaRotation. As for "being good at this", I don't even come close to alot of other members here. Your problem however, was purely mathematical. An understanding of vectors (these are pretty simple, google it) was the only real need.
  • 16/01/2009 2:29 In reply to

    Re: 2D Tutorial: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Thanks a lot for all the help... I really needed it.  The last thing I ask, is that you tell me how to make my cannonballs faster, becuase i can't seem to find that part of the code where that integer is listed. I found the speed of the alien ships, but for some reason I can't seem to find the cannonball velocity...
  • 16/01/2009 2:32 In reply to

    Re: 2D Tutorial: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    ps: where should i put
    FireCannonBall(0.0f);  
    FireCannonBall(MathHelper.PiOver4 / 2);  
    FireCannonBall(-MathHelper.PiOver4 / 2); 
  • 16/01/2009 2:58 In reply to

    Re: 2D Tutorial: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    In your Update method, there should be something dealing with button presses like "if(keyboardState.stuff && !previousKeyboardState.stuff)" and in that you have a call to FireCannonBall() Change that to FireCannonBall(0.0f) if you are using my updated function.

    In my function the speed is set at 15.0f and change it higher or lower for faster or slower.

    Now that you see how simple it is you need to focus on learning the fundamentals of programming, be it C#, C++, C, Java, etc. This shouldn't take too long depending on your dedication. After that you will be able to begin programming your own simple games, but until then you will only frustrate yourself when your code isn't working.
  • 17/01/2009 6:08 In reply to

    Re: 2D Tutorial: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    OOOOKKKK... Now I get it.
    It took me way to long to get what you were saying.. sorry.
    Um thanks for all the help, and I really feel like I'm starting to understand some of these phrases and things in the code.
    Thanks
  • 17/01/2009 22:14 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    TNTWolverine:
    Ahhh yes, I remember this problem. The solution is quite simple, here is my updated code...
    public void FireCannonBall(float deltaRotation)  
            {  
                foreach (GameObject ball in cannonBalls)  
                {  
                    if (!ball.alive)  
                    {  
                        Vector2 cannonDirection = new Vector2((float)Math.Cos(cannon.rotation), (float)Math.Sin(cannon.rotation)); //cannon direction  
                        Vector2 ballDirection = new Vector2((float)Math.Cos(cannon.rotation + deltaRotation), (float)Math.Sin(cannon.rotation + deltaRotation)); //cannon direction + added rotation  
                          
                        ball.alive = true;   
                        ball.position = cannon.position - ball.center + (cannonDirection * (cannon.sprite.Width / 2)); //start at end of cannon  
                        ball.velocity = ballDirection * 15.0f; ; //direction of ball with speed mult. of 15  
                        return;  
                    }  
                }  
            } 


    You really hit the nail on the head with this one, TNT.

    I had just coded the tutorial and was noticing the problem of the ball leaving the base of the cannon rather than the barrel. I'm an experienced C# coder (notsomuch XNA yet), and I fully understand the coding methods you used, but I'm a little rusty with my trig functions.

    The parameter deltaRotation is simply to allow you to fire multiple shots, is it not? I'm figuring it is, but I'd like to make sure before I remove that functionality. I'm not really all that interested in the 3 shot idea, but it's still freaking nifty. And on a side note, using "(MathHelper.PiOver4 / 4)" or "(MathHelper.PiOver4 / 8)" makes the 3-shot spread a little tighter (11.25 degrees and 5.625 degrees of deviation respectively, versus the original 22.5 degrees), and imho a little bit handier for killing aliens.

    Also, I'm not really understanding the calculation that is taking place in "(cannonDirection * (cannon.sprite.Width / 2))". How does the direction vector of the cannon multiplied by half the sprite's width translate to being the end of the barrel? I understand that multiplying the magnitude times the direction vector will get result in another vector, but how did you figure out the magnitude, i.e. "(cannon.sprite.Width / 2)", that you would need to reach the desired point?

    Excellent post, TNT. I think this needs to be stickied in the forums for sure. Thanks.

    -DND
     
    P.S.- You are a life saver!

  • 18/01/2009 0:44 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Ok. Um I really need help with something. I have started doing the extra credit section of the tutorial, and I have run into a slight problem.
    At the section of the code in ParticleSystem.cs, where:
    protected virtual void InitializeParticle
    i was given the error:
    Inconsistent accessibility: parameter type 'WindowsGame1.Particles.Particle' is less accessible than method 'WindowsGame1.Particles.ParticleSystem.InitializeParticle(WindowsGame1.Particles.Particle, Microsoft.Xna.Framework.Vector2)'
    I am the definition of beginner at programming with C# (or any other language), so I have literally no idea how to fix this and where i went wrong.  I went back in the video to compare my code to his, and it matched it. 
    I really need help, because without guidance, I will never fix this problem.
  • 18/01/2009 17:20 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    DotNetDude, you are correct in that rotation is only used for firing at different angles and also correct that "/ 8" would make it tighter than "/ 4". The cannonDirection * (cannon.sprite.Width / 2) is pretty simple once you look at the cannon sprite. The cannon sprite's image is actually twice the size of the cannon, meaning the width of the image divided by 2 is the length of the barrel. Multiplying that length by a direction vector gives us a new vector at the end of the barrel. The ball.center is also subtracted for that little bit of precision. When I get home later this week I am going to write the code out in simpler steps so it will become more apparent to everyone what I was doing.

    Narj, I think this is because he didn't make the class "public" in his video. Check his downloaded source code to see any differences there.
  • 18/01/2009 18:29 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Wow, thanks TNT! That's a pretty neat trick. One question though. This may be out of place in this forum, but how do you add the "blank space" to the left of the sprite? I was messing around in Gimp and didn't see how to widen a layer or anything.

    Thanks again.

    -DND
  • 19/01/2009 4:12 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    I generally don't play around with images, just code (the cannon comes as a sprite that wide). If I do though, I use Paint.NET because it is a small, simple, and versatile program. In that I would find the "resize canvas" option or something like that. So sorry that I'm not much help there, but if Gimp is nice software I'm sure it isn't too hard to do that...
  • 19/01/2009 4:45 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    I'm a fan of Gimp, but for simple photo manip, it's pretty annoying. I'll check out Paint.Net to see if it can do what I need it to do. Thanks a ton for your help, TNT!

    -DND
  • 21/01/2009 2:48 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Umm... I'm getting really desperate now guys, so if you could just go look at my last post and help me out, that'd be great.
  • 21/01/2009 3:13 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    If you're still on the particle thing, I already responded to that. it should be "public class Particle" and not just "class Particle"

    EDIT: The first post's code has been updated so it is easier for others to recognize what is going on...
  • 21/01/2009 23:16 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Ok thanks. sorry for making you repeat yourself.  Now i have another quick question. It said that NullReferenceException was unhandled for this piece of code:
    enemyCannonBalls[i] = new GameObject(Content.Load<Texture2D>("Sprites\\cannonball"));
    and it said as a suggestion that i add new next to GameObject, but i have obviously already done this.
    please help me.
  • 22/01/2009 1:46 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    Compare your code to his code. The source is there for a reason...

    http://www.xnadevelopment.com/tutorials/2dextracredit/2DExtraCredit.zip
  • 24/05/2009 2:28 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    How do I reset my cannon back to semi-auto?
  • 24/05/2009 2:30 In reply to

    Re: 2D Tutoria: CannonBalls coming from the base of the Cannon, not the barrel.. NEED HELP!

    ...nevermind I found it
Page 1 of 1 (22 items) Previous Next