# First DX9 Game Questions...

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

## 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 on other sites
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 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 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 on other sites
Quote:
 Original post by MJPIf you just want the arrow to fly straight towards the point where the mouse clicked, then the direction you want is the vector . 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 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 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 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 on other sites
Quote:
 Original post by PittaI 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 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 on other sites
Hot damn! I did it!

I didnt need the extra multiplication with the deltaTime thing because the function is only called when the tickCount is met in the gameloop...

What doesn normalizing the vector for the direction do? I took that line out to experiment, and it was all kinda of messed up...

Is it better to use tickcount in a for loop like I showed above? or should I control everything individualy with its own couters... This seemed more efficient to me...

##### Share on other sites
So I am feeling fairly good about this now... I even figured out how to make an installable package with VS08, so I can send it easily to my family...

One thing Id like to know... How do I code my game so it calls on resources form a folder, not the root of the applicaition...

say I have:

\my project folder
|
-hit.wav
-code files
-Art (folder)
|
- bounce.wav

right now when loading content to devices, I use "hit.wav" for instance.... but if id like to try and kee things organized, how do I call on bounce.wav?

Good morning!

##### Share on other sites
Quote:
 Original post by PittaWhat doesn normalizing the vector for the direction do? I took that line out to experiment, and it was all kinda of messed up...

When you normalize a vector, the length (magnitude) of that vector becomes 1. For a velocity vector, the magnitude is equal to the overall speed. This means when you take normalized direction vector and multiply it with a scalar (your speed value), you get a velocity vector whose magnitude is equal to that scalar. If you weren't to normalize the vector, the overall speed is going to be equal to the magnitude of the vector multiplied with the value of the scalar. For instance say you had the arrow starting point as <0,0> and the mouse click point as <10, 10>: you would subtract to get the direction vector, which is <10,10>. The magnitude of that vector is sqrt(100*100 + 100*100) which is approximately 141. This means your final speed would be off by quite a bit!

##### Share on other sites
It's good to see someone excited about learning all the new stuff. Congrats!

Why should a direction be a normalized vector? In this particular case I recommended it so you can control the movement. If you don't know the length of direction, then direction*speed may be a different amount of movement each frame. With direction normalized, then direction*speed is always going to be a movement of speed (units/sec) in the desired direction. If it's not normalized, the movement will be speed*length-of-direction-vector in the desired direction, whatever "length-of-direction-vector" happens to be at the moment.

How to load resources from a directory of choice? One method is to use getcwd in the intialization of your app. Something like:

1. put your resources in a subfolder of your project folder named, for instance, "resources."

2. at the start of your app, call getcwd() and get (for instance) "c:\\projects\\myproject\\release\\" (from the directory where the .exe was launched.

3. search for "myproject" in the string, truncate the string and append "resources\\" to end up with "c:\\projects\\myprojects\\resources\\". Store the string as resourcePath or similar. If you don't find "myproject" in the string, emit an informative error message and quit the app (or something relatively friendly to the user).

4. elsewhere in your app, to load a resource named "bounce.wav" from the resource folder, make a copy of "resourcePath" and append "bounce.wav" for a resource path of "c:\\projects\\myproject\\resources\\bounce.wav"

5. Use the string to open the resource, load a mesh, etc.

##### Share on other sites
alot of new commands in there buckeye... thanks! Im not sure I follow all of it, but back to the searches for now and testing it in my app!

How do you guys share finished products? compare and share code... ect?