C# Rotation



  •  This year i took up computing at A level and have been learning how to code in C# so im quite new to it.

    what im trying to do now is make asteriods, i have a ship that turns at 45  degrees when you press left or right and shoots in the direction its facing.  the problem with this is that i would like to make it rotate in smaller steps like the original but this would be impossible with my current way of determining which way the shot is fired.

    heres a little snippet. 

     ....

                    if ((bulletRotation > (5.496)) || (bulletRotation < 0.785))//find the direction it is facing
                    {
                        bullet.Y -= bulletSpeed;//set the speed accordingly
                    }
                    if ((bulletRotation > 4.711) && (bulletRotation < 5.496))
                    {
                        bullet.Y -= bulletSpeed;
                        bullet.X -= bulletSpeed;
                    }
                    if ((bulletRotation > 3.926 + 0.785 / 2) && (bulletRotation < 4.711))
                    {

                        bullet.X -= bulletSpeed;
                    } ... 

     

    im sure theres an easyer way of doing it but i cant think of any, so realy what im asking for is a way to determine what direction the shot should go depending on the direction the ship is facing.

     

    thanks for the help in advance. 


  • :belt_onion:

    You could use some basic vector math in order to accomplish these things. In your current system, you seem to be working with these directional vectors [x, y] where x and y form any combination of -1, 1 and 0, such as [1,-1] or [1,0]. Formalize the use of vectors a bit and things will get a lot easier

    Google is your friend

     



  •  Also, try to use pi / 4, 5 * pi / 4, etc instead of 0.785 and 3.926. It's slightly more accurate and a lot easier to read for people. It might be easy to recognize 0.785 = 3.14 / 4 so you mean "45 degrees angle", but the decimal forms of, for example, 5*pi/6, is kinda hard to see.



  • Sounds like this is 2-dimensional and you already have the bulletRotation angle, so just do this:

    bullet.Y += bulletSpeed * sin(bulletRotation);
    bullet.X += bulletSpeed * cos(bulletRotation);

    and you're done for all possible angles (assuming angle 0 is pointing in positive x, and positive angles are counter-clockwise).  None of that nasty conditional logic required =)


  • :belt_onion:

    @too_many_usernames said:

    so just do this:

    bullet.Y += bulletSpeed * sin(bulletRotation);
    bullet.X += bulletSpeed * cos(bulletRotation);

     

    too_many_usernames, proudly doing student's homework since december 2005 :-)



  • @bjolling said:

    too_many_usernames, proudly doing student's homework since december 2005 :-)



    Generally I like to think I'm pretty selective in my "homework support".

    Although I guess in hindsight I was sloppy in this case in that I just threw code out there instead of my usual approach of asking questions like "Since you know the angle already, what functions can you use to obtain the cartesian direction you need? Consider using that method instead."

    I suppose I could say I was being generous, or maybe I could say I was being evil in that in giving code that someone doesn't understand they'll just shoot themselves in the foot later.  The scary part is, even I don't know which one of those is more accurate...



  • @too_many_usernames said:

    I suppose I could say I was being generous, or maybe I could say I was being evil in that in giving code that someone doesn't understand they'll just shoot themselves in the foot later.  The scary part is, even I don't know which one of those is more accurate...

    If it helps, he is only an Iranian long range missile guidance system engineer, not a student.


  • :belt_onion:

    @Farmer Brown said:

    @too_many_usernames said:
    I suppose I could say I was being generous, or maybe I could say I was being evil in that in giving code that someone doesn't understand they'll just shoot themselves in the foot later.  The scary part is, even I don't know which one of those is more accurate...
    If it helps, he is only an Iranian long range missile guidance system engineer, not a student.
    I'm already imagining this guy shooting himself in the foot with his long range missile.

     



  • @bjolling said:

    I'm already imagining this guy shooting himself in the foot with his long range missile.

    "You dumb bastard! You hit France instead of Israel! Where did you get this code?"

    "Roger, set coordinates for Ohio, USA."

    "Praise Allah!!!"


  • :belt_onion:

    @Farmer Brown said:

    "Praise Allah!!!"
     

    already an improvement to their previous launch platform

    Iraqi Scud Missile



  • Thanks allot for the help, and do not worry i think i understand the code that you typed, well heres how i think it works:

    bullet.Y += bulletSpeed * sin(bulletRotation);//finds the y-side  of the triangle with hypotenuse of length 'bulletRotation'
    bullet.X += bulletSpeed * cos(bulletRotation);//same but for the x-side.

    is that right? if not tell me but i think that looks about right. i knew there would be a way of doing it i just couldn't think of it.

    also, when i try to type it into my compiler i get a few errors, the first was to do with the sin and cos, but changing it to Math.sin/Math.cos solved that the rest is to do with converting from float to int, i know that to add a number to a float i need to put in a cast e.g.:

    float floaty=o;
    ....
    floaty+=1f;
    ...

    or something like that.

    my problem is that bulletRotation needs to be a float to be passed into the part of the program that draws the bullet but bullet.Y wont let me add a float to it. so i am just asking is there way of converting bulletSpeed * cos(bulletRotation); to a int?

     


    [ Mod note: formatting added. Have a nice day. ]



  • for some reason all my spacing was deleted and now i cant edit my post >.> how come it didn't do that to my first post but it did to my second?



  • @Countspatula said:

    for some reason all my spacing was deleted and now i cant edit my post >.> how come it didn't do that to my first post but it did to my second?

    Welcome to the forums!



  • @Countspatula said:

    so i am just asking is there way of converting bulletSpeed * cos(bulletRotation); to a int?
    Whoa whoa whoa, slow down there, fella. You're saying you need to convert data from one type to another? That sounds unreasonably complicated and probably impossible

    Seriously, though, if you can't get your head around something as simple as type conversion I think you really ought to take a step back and relearn the basics. I know that you're trying to learn the language, but things like type conversion are common between all languages (implicitly in some, before Morb comes in here and whines about dynamically typed languages), and you obviously need to know about it to make this game of yours.

    I'm not going to send you the codes, but what you should be Googling for is called "casting" and "type conversion" in C#.



  • @Welbog said:

    @Countspatula said:

    so i am just asking is there way of converting bulletSpeed * cos(bulletRotation); to a int?
    Whoa whoa whoa, slow down there, fella. You're saying you need to convert data from one type to another? That sounds unreasonably complicated and probably impossible

    Seriously, though, if you can't get your head around something as simple as type conversion I think you really ought to take a step back and relearn the basics. I know that you're trying to learn the language, but things like type conversion are common between all languages (implicitly in some, before Morb comes in here and whines about dynamically typed languages), and you obviously need to know about it to make this game of yours.

    I'm not going to send you the codes, but what you should be Googling for is called "casting" and "type conversion" in C#.

    Or look for a function in Math that does rounding to the nearest integer or something.


  • @Countspatula said:

    Thanks allot for the help, and do not worry i think i understand the code that you typed, well heres how i think it works:

    bullet.Y += bulletSpeed * sin(bulletRotation);//finds the y-side  of the triangle with hypotenuse of length 'bulletRotation'
    bullet.X += bulletSpeed * cos(bulletRotation);//same but for the x-side.

    is that right? if not tell me but i think that looks about right. i knew there would be a way of doing it i just couldn't think of it.

     

    Hrm...aside from the integer to float conversion, I might also recommend a review of the sine and cosine functions. Your explanation in the added comments is almost correct, but close only counts, as they say, in horeshoes and hand grenades.



  • woo i think ive figured it out:

     

    bullet.Y += (int)(bulletSpeed*Math.Sin(bulletRotation)); 

    bullet.X += (int)(bulletSpeed*Math.Cos(bulletRotation));

    it works perfectly,my problem with casts was that i was confusing them with something else, when i made my floats for rotation whenever i added/subtraced from them i had to put and f at the end of the number eg:

    float bob=1;

    bob+=1f;

    why did i have to do that anyway? it woudl be nice to know :D

     thanks alot everyone! all i need to do now is add some asteriods and its all done.

     EDIT: what the hell it didnt mess up my spacing this time >.<



  •  I don't know.  This is valid C# that compiles:

     

    float f = 1;
    f += 1;



  • That code will compile, because int is implicitly convertible to float, but I suspect what Countspatula was doing was trying to at some point or other is stick a decimal point in there, like so:

    bob += 1.5;

    The constant 1.5 is not a float, but a double (like a float, but twice as precise!), and due to the loss of precision that could occur, C# won't let you do that unless you tell it to with a cast:

    bob += (float)1.5;

    or  just make the constant a float to begin with (that's what the f means):

    bob += 1.5f;

    Rather annoying in game programming I know, as games tend to use floats because they're smaller and easier to process (32 bits instead of 64), but the math routines use doubles to be precise... many game libraries however come with float-based math libraries (which may or may not actually be float-based - they might just do a lot of casting and calling builtin functions in the background, defeating the purpose of the whole "easier to process" thing...)

    I think XNA has a class called MathHelper or something which provides such functionality, but I might be thinking of some other library...



  • Oh right, did know that constants like that even had a type but i guess it makes sence, thanks alot for explaining it.  and yeah, i used a bullet point i forgot to put it in my example though, sorry.

     



  • @Countspatula said:


                    if ((bulletRotation > (5.496)) || (bulletRotation < 0.785))//find the direction it is facing

                    {
                        bullet.Y -= bulletSpeed;//set the speed accordingly
                    }
                    if ((bulletRotation > 4.711) && (bulletRotation < 5.496))
                    {
                        bullet.Y -= bulletSpeed;
                        bullet.X -= bulletSpeed;
                    }
                    if ((bulletRotation > 3.926 + 0.785 / 2) && (bulletRotation < 4.711))
                    {

                        bullet.X -= bulletSpeed;
                    } ... 

     

     

    WTF is  5.496, 0.785, 4.711, and 3.926 + 0.785 / 2. Just some magic numbers you found in the gutter?

     I didn't quite follow what everone else said. But one problem with the above approach is bullets shot at a 45 degree angle will travel faster than bullets fired straight. 



  • meh, my $.03 says "define problem"

    I personally like for my games to give me an edge over the computer.  Hopefully he works out how to give the player faster bullets and the computer slower, that would be nice...


Log in to reply