|
Any Direction in 2d?? |
dylan sack
Member #6,590
November 2005
|
So, I have no idea even how to go about this.. So sorry I don't really have any source to put, so this'll mainly be a logic or pseudo-code thread. I've seen this done before, so i'm not just insane, how would I go about making a 2d topdown game (not iso) that you can face and shoot in any direction? The most desirable would probably be, your character would always face your cursor(which would be a gun aim picture thingy reticule) and you could turn around like that, and left mouse click would shoot. W would be forward, S be back, a and d be strafe and you know. Just kinda throwing that out there to get the feels of the controls I guess. Anyway, back to the point. The only way i've thought of for this would be manually handelling the shoot function for every single possible direction you could face.. but that's.. that just can't be the only way, heh.. Would it have to involve some 3d aspects? I don't understand how I could update a bullet and read it to see which direction to shoot in any way in 2d. Am I crazy and the games I saw that did this were not 2d but iso or something?? EDIT: http://www.gamedev.net/community/forums/topic.asp?topic_id=390918 <-- that game does it and it's 2d topdown, so I can't be insane. But I still don't know how to go about it |
CursedTyrant
Member #7,080
April 2006
|
If I understand correctly, you need something like this: void CEnemy::Move(float nX, float nY) { float Angle = atan2(nY-Y, nX-X)*180.0f/M_PI; X += cosf(Angle*M_PI/180.0f)*Speed; Y += sinf(Angle*M_PI/180.0f)*Speed; } What it does is make the CEnemy object move towards the (nX; nY) coordinates. And in the bullet object you just do something like: CBullet::CBullet(float nAngle) { Angle = nAngle; //other stuff here } void CBullet::Move() { X += cosf(Angle*M_PI/180.0f)*Speed; Y += sinf(Angle*M_PI/180.0f)*Speed; } That should work fine. --------- |
Mark Oates
Member #1,146
March 2001
|
yes, what Mr. Tyrnat said. It all basically comes down to angles, sin, and cosine. I recommend this tutorial to help clear things up: -- |
Jonatan Hedborg
Member #4,886
July 2004
|
Or you could do it slightly less confusing (and faster) by storing the angle as radians instead... void CEnemy::Move(float nX, float nY) { float Angle = atan2(nY-Y, nX-X); X += cosf(Angle)*Speed; Y += sinf(Angle)*Speed; } you could also use pure vector maths for this problem;
Untested, but that ought to work too. Not sure which one would be faster, but both has some merits. And as for the bullet, once again, store the angle in the native form: Or use vectors In this case vectors would be faster and simpler (heck, you can even store speed right in the tx/ty pair by multiplying them with it in the start).
|
dylan sack
Member #6,590
November 2005
|
Wow, three answers, talk about service ^_^ Thanks for the help guys i'm going to go try to work that into my game right now. |
Johan Halmén
Member #1,550
September 2001
|
There are only a few basic facts when talking about 2D coordinates and directions. The rest derives from the basics. In Allegro graphics you have one coordinate growing from left to right, usually called x, and another growing from top to down, usually called y. The rest, especially the direction stuff, is up to you, as I see it. You can use radians or Allegro angle units for the direction. You can decide that zero be to the right, to the left, up or down. And you can choose that growing direction is like rotating clockwise, or counter clockwise. I guess there's a standard used in games, but I've never bothered to figure out that. I might use the stuff differently in different games, because I'm a lousy coder what comes to reusing stuff. Whatever you choose, it affects the trigonometric formulae, which should be clear for you. Jonathan's code said: It works, but it computes the cos(Angle)*Speed each time the bullet moves. It should compute it only when either Angle or Speed is altered. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
dylan sack
Member #6,590
November 2005
|
Ok tried a few of the methods out, and i'm having weird errors about converting to fixed or whatever. Can anyone explain this?
I'm pretty new to using any kind of maths in games and just coding games in general.. (i'm only a freshman in algebra 2 >.<) So if I did something realllly dumb try to forgive me |
imaxcs
Member #4,036
November 2003
|
Include cmath in your source-file: #include <cmath>
I hope that helps...
|
CursedTyrant
Member #7,080
April 2006
|
BTW, If you're using floats use cosf and sinf. Like this: float Angle = atan2(nY-player.y, nX-player.x); player.x += cosf(Angle)*Speed; player.y += sinf(Angle)*Speed; You could also use atan2f if you really want to. --------- |
Johan Halmén
Member #1,550
September 2001
|
dylan said: So if I did something realllly dumb try to forgive me We forgive you, but I don't see the point in it ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
dylan sack
Member #6,590
November 2005
|
Ok so all these variables are kinda confusing me, what exactly needs to be added to if say I wanted to make a keypress have you rotate to the right? I experimented around with adding values to some of the variables and nothing happened, given I never really took the time to fully understand the code you guys gave me, i'm just kinda using it But I donno, I think i'm just generally confused about it still.
|
Johan Halmén
Member #1,550
September 2001
|
There's no use in trying to get our examples work if you don't understand them. If it is that hard, you simply have to step back some steps and do something simpler. And try to understand every step. I could point out that your class has members x and y but the member function move()declares the variables X and Y, which you use in the moving. Well, it doesn't work. And if you don't get it, don't waste time in trying to get it work. Spend that valuable time instead in studying more basic things. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
Mark Oates
Member #1,146
March 2001
|
Quote: And if you don't get it, don't waste time in trying to get it work. Spend that valuable time instead in studying more basic things. Johan speaks the truth. But I believe that you can figure this part out. Let me try and help. If you change your player class to contain these variables it should make things much more clear:
so in your logic loop do something like
-- |
dylan sack
Member #6,590
November 2005
|
Thanks for the help, and I know I should go back and really figure it out and understand, which i'm about to do. I was just really excited and kinda rushed because I wanted to see some real progress in my game. Feels like i'm getting somewhere (even though I got a lot of help on this part of it ) The thing is, if I declare x and y as floats, then my compiler says when I try to draw the sprite, you can't draw the sprite at a float location. So maybe I could do another x and y that are ints and make them equal to the float x and y's and draw them there? Thanks for all the help btw guys. |
Mark Oates
Member #1,146
March 2001
|
float f; int i = (int)f; you have to 'cast the float as an int' -- |
dylan sack
Member #6,590
November 2005
|
Thanks for all the help, it works perfectly. I'm pretty sure I can figure out rotating the sprite as it turns by myself. It works perfectly besides that. I'm going to sit out a while and just read the code over and over untill I viualize how it perfectly works in my head. |
Mark Oates
Member #1,146
March 2001
|
haha, ah yes. I know the feeling. It's like taking a dump after being constipated for a long time. "I just want to take a break for a while" -- |
dylan sack
Member #6,590
November 2005
|
Yea, but then you get constipated again coughrotatespriteisdumbcough and you must learn from your past and get through it on your own :O |
Mark Oates
Member #1,146
March 2001
|
otatespriteisdumb?... Oates' Sprite is Dumb!? YOU DON'T LIKE MY AVATAR!!!?!? ... just kidding. -- |
dylan sack
Member #6,590
November 2005
|
hey I have one last question , how come this rotate sprite thingy isn't working. It compiles perfectly, and I have no idea why it's not rotating with the turning function. By all means it should, I know the problem isn't it getting drawn cause I tried it with the x and y a little offset and it showed up, it just doesn't rotate...
I have no idea why coding/life hates me.. |
Mark Oates
Member #1,146
March 2001
|
what does your rotate sprite function look like? -- |
Johan Halmén
Member #1,550
September 2001
|
Your code said: This function seems to be just a separate function, not a member of the class, right? In this function you declare the variables x, y and angle. These variables live only inside the function. In line 2, x is declared and gets its value from player.x, which implies that player is a global object. player.x itself is not changed, not truncated or rounded or anything. After line 4 all three variables, x, y, and angle come to a peaceful death and won't affect anything else in your program. In my former post I tried to point out a very similar thing. It looks like you're not clear on how functions work and where local variables and global variables live (the scope of the variables). What comes to casting, I have a bad habbit of not casting floats to ints before drawing. I get lots of compiler warnings, but they are only warnings. The program still works. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
Tobias Dammers
Member #2,604
August 2002
|
Always treat warnings as errors. Best case, they are a sign that you are debugging (and have commented out some code). Worst case, they are causes for real errors or bugs. Semi-bad case, they show bad style. float f = some_number_you_choose; int i = (int)floor(f);
@dylan: You need to understand the concept of 'scope'. The scope of a variable is the part of the code during which it is valid. There are, basically, 4 kinds of variables in C++: Any two variables may have the same name, provided their scopes don't overlap. It is even possible to have variables with identical names whose scopes overlap (but are not identical), but this causes a whole bunch of problems and is generally strongly advised against. Example:
--- |
dylan sack
Member #6,590
November 2005
|
Ok considered what you guys said a bit, and have revised my code.. now it er.. doesn't work still, but i'm pretty sure I stay inside my scopes hah. Anyway, i'm going to post my code again, how about this time you could all just like.. hint towards what i'm supposed to do so I can figure out something on my own for once heh. Anyway, at least i've learned a lot from this about coding in general, right?
|
Onewing
Member #6,152
August 2005
|
Quote: doesn't work still What "doesn't work still"? The rotation? What is it doing? Anyways, where did you learn to do classes? I'm not implying anything, other than I get the feeling you don't completely understand them. BTW, weren't you working on a side-scoller? How did that turn out? ------------ |
|