Jump to content
  • Advertisement
Sign in to follow this  
Pitta

First DX9 Game Questions...

This topic is 3689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm green to DX in general, just got started a few weeks ago. I currently have a project I’m working on that is akin to a castle defense game. I thought it would be great to make and learn about different stuff. I have a standard 800x600 window and a sprite for the mage on the tower at the middle/bottom of the screen. I also have a sprite that is used for the "magic arrow" that appears 'under him' (because the mage is drawn last in the scene) and flies across the screen in the direction you click, or at least that’s the idea. I have already made a cursor sprite, and get that tracking perfectly on the screen. I also have it setup to read mouse clicks, and to limit the number of arrows to one on screen. I also was able to test it all by making an arrow fly straight up the middle no matter where I clicked. Now I want it to fly where I click, and continue till it goes off-screen (i can handle the off-screen part), but am having a big issue with the direction it flies when you click... Currently I have the start point set with variables in the arrow sprite (arrow.startx & arrow.starty), and I track the mouse position clicks with the cursor sprite by setting the coords at click time to arrow.dirX and arrow.dirY. I know I need both of these values, but can’t get the algorithm correct I guess, half the time the arrows flies downwards haha! Am I in the wrong place? Am I in over my head? I passed trig last term in school, but can’t make heads or tails of this... Thanks in advance for any constructive help! [Edited by - Pitta on July 10, 2008 12:44:15 AM]

Share this post


Link to post
Share on other sites
Advertisement
I use this normally for moving in 2D space (x,y and object heading)


#region Move NPC
public void Move(int locx, int locy)
{
if (locx > this.locx && locy > this.locy) // +1, +1
{
this.heading = 3;
}
if (locx < this.locx && locy < this.locy) // -1, -1
{
this.heading = 7;
}
if (locx > this.locx && locy == this.locy) // +1, 0
{
this.heading = 2;
}
if (locx < this.locx && locy == this.locy) // -1, 0
{
this.heading = 6;
}
if (locx == this.locx && locy < this.locy) // 0, -1
{
this.heading = 0;
}
if (locx == this.locx && locy > this.locy) // 0, +1
{
this.heading = 4;
}
if (locx < this.locx && locy > this.locy) // -1, +1
{
this.heading = 5;
}
if (locx > this.locx && locy < this.locy) // +1, -1
{
this.heading = 1;
}

this.locx = locx;
this.locy = locy;
}
#endregion




Sorry its in C# but the math should be the same

this. = the object
locx and locy = next location

you could do the math automaticly by adding a loop to go true each move.

i know this is kinda rough, but the x,y calculation is what u need, so like doing a "while" loop or whatever you need


while(this.x < endloc.x && this.y < endloc.y)
{
// calculate next movement
// is it +x, -x or no x movement, same with y
// move
int x = 0;
int y = 0;


if(this.x < endloc.x)
x = -1;
else if(this.x > endloc.x)
x = 1;
else
x = 0;

if(this.y < endloc.y)
y = -1;
else if(this.y > endloc.y)
y = 1;
else
y = 0;

int nextx = this.x + x;
int nexty = this.y + y;

Move(nextx, nexty);
}



just done in head, didnt check it might need some speed stuff added too

Share this post


Link to post
Share on other sites
If you just want the arrow to fly straight towards the point where the mouse clicked, then the direction you want is the vector <mouse.x - arrow.startx, mouse.y - arrow.start.y>. You also need to normalize it too, before you can use it.

It doesn't look like you're using the D3DX vector classes, in which case I suggest you do. It will definitely make things much easier for you as you progress. For example the code to do what I was talking about looks like this:


D3DXVECTOR2 mouseClickPos (mouseX, mouseY);
arrow.direction = mouseClickPos - arror.StartPos;
D3DXVec2Normalize(&arrow.direction, &arrow.direction);

Share this post


Link to post
Share on other sites
hehe yeah mine might be a bit overkill, thats just how i do it in plain code anyways :)

also allows you to "slow" projectile

but, its limited to 8 directions

\|/
-o-
/|\

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
If you just want the arrow to fly straight towards the point where the mouse clicked, then the direction you want is the vector <mouse.x - arrow.startx, mouse.y - arrow.start.y>. You also need to normalize it too, before you can use it.

It doesn't look like you're using the D3DX vector classes, in which case I suggest you do. It will definitely make things much easier for you as you progress. For example the code to do what I was talking about looks like this:


D3DXVECTOR2 mouseClickPos (mouseX, mouseY);
arrow.direction = mouseClickPos - arror.StartPos;
D3DXVec2Normalize(&arrow.direction, &arrow.direction);


Crap I didnt even think about that.... Im over ehre trying to figure all the x's and y's, and vectors make much more sense... I think im going to go try that...

What you mention seems to me that the arrow will stop at the point I clicked... any thoughts as to how to allow the arrow past that? Ill post working code when I get some... off to VS08...

(what does normalizing do? I asusme it smooths out the travel so its not all jittery...)

Thanks alot!

Share this post


Link to post
Share on other sites
stepping through the debugger, I am getting the arrow.direction vector to get coords now... I just need to get the sprite to travel across that line I guess...

Share this post


Link to post
Share on other sites
To get the arrow to "travel" along the line, decide how fast you want it to move.

Make sure your direction vector is normalized. Then, for each increment of time (deltaTime):

newPosition = oldPosition + direction*speed*deltaTime;
oldPosition = newPosition;


Detect if newPosition is offscreen now or it's hit something. Take the appropriate action.

Otherwise, rotate the arrow to direction, translate it to newPosition, and render it. Now you're ready for the next increment of time.

That's the beauty of vector math.

Share this post


Link to post
Share on other sites
@ Buckeye

Sorry about the late reply.. had to take a break... I think I may be getting ahead of myself in class... we are learning vectors, but not to this degree... I can see once I get the nhang of it, how much nicer it would be...

I am understanding that newPosition, oldPosition, direction are all vectors of the D3DXVECTOR2 Variety?

Speed is any int or double I choose, and deltaTime is something I can call on, or do I need to start it and catch it like srand?

(forgive me green-ness, and I really appriciate the help... this is driving me batty)

Thsi is all I know of timing.... I am using this to control the animation rate of my sprites...

if (GetTickCount() - start >= 30)
{
int n = 0;
//reset timing
start = GetTickCount();

Move_Mage();
Shoot_Arrow();
Move_Arrow();
Move_Mouse();

for(n=0; n<NUM_MONSTER; n++)
Monster_Move(n);
}




[Edited by - Pitta on July 9, 2008 11:06:50 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Pitta

I am understanding that newPosition, oldPosition, direction are all vectors of the D3DXVECTOR2 Variety?

Speed is any int or double I choose, and deltaTime is something I can call on, or do I need to start it and catch it like srand?



Yup, those 3 are vectors. Speed will be some value of the unit pixels per second, so if it's 10 your arrow will go 10 pixels every second. deltaTime would be the amount of time that's elapsed since the last frame, which you can calculate using a timing function like timeGetTime or QueryPerformanceCounter. If you just do a search through the forums on "timing" or "game timing" you should find some threads and articles explaining how to do this.

Share this post


Link to post
Share on other sites
sweet! looks like another sleepless night... so much work for an arrow! Im so crazy excited about getting this working I wanna show everyone LOL.... Ill search around the forums, and ill be reporting back here soon...

This community is great! I stumbled upon it today and everyone has been super helpful.

Thanks for not flaming me!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!