shoot bullet towards mouse pos

Started by
12 comments, last by unbird 13 years, 3 months ago
I'm having trouble shooting my bullet sprite towards the mousepos x and y at the same time in a straight line in c++ dx9.
I guess it's easy coding, but I haven't done this before, and can't really find a answear I understand in these advanced "tutorials" all over the place.


--------------------------------
MSVC++ 2010 Express
C++
DirectX9
-Zaerdna
Advertisement
Is your game 2D or 3D? For 2D, you can just take the mouse X and Y coordinates to get the world coordinates corresponding to that point, in 3D you want to look into "picking" to generate a 3D line from your 2D mouse position.
Are you working in 2D? 3D? How are you determing the sprite location?

First, ensure you correctly convert the mouse position to world position. Or, if you're working in window client coordinates, make sure, if necessary, the mouse position has been converted from screen to client coordinates.

A general method is to use a start position and a unit direction vector.

//.. when bullet is fired:
D3DXVECTOR3 startPos = gunLocation;
D3DXVECTOR3 dir = mousePos - gunLocation;
D3DXVec3Normalize(&dir,&dir);
//.. each frame using deltaTime between frames
D3DXVECTOR3 bulletLocation = startPos + bulletSpeed*deltaTime*dir;


( ninja'd)

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Working with 2d
I draw my bullet like this:


d3dbullet->Begin(D3DXSPRITE_ALPHABLEND);
D3DXVECTOR3 bul_center(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 bul_position(bul_x, bul_y, 0.0f);
d3dbullet->Draw(bullet, NULL, &bul_center, &bul_position, D3DCOLOR_ARGB(255, 255, 255, 255));
d3dbullet->End();


Basically I only need help calculating the bullet's path to go when mouse is pressed, i want to move bullet (wich don't need rotation cause it's a square) from the pistol, and make it move to exactly where I press, and continue that path..
-Zaerdna
i want to move bullet ... where I press, and continue that path
See my post above.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I can't get your code working.
How do I translate my MousePos (POINT) into float?
also, "deltaTime" is undefined.

I only need the code to:
-Translate POINT to FLOAT.
-Set bullet's path (moving towards MousePos)
I don't understand all that fancy code, if you use something that is really advanced, or that required something done before using it, please tell me how.
Tnaks.
-Zaerdna
I don't understand all that fancy code, if you use something that is really advanced, or that required something done before using it, please tell me how.
The principles involved in what I posted are very basic, Zaerdna, particularly with respect to setting a float value from a LONG value. That's basic C++ code.

E.g.,

D3DXVECTOR3 mousePos;
mousePos.x = mousePoint.x;
//.. etc.

Part of learning how to program is being able to write code from descriptions and samples. That will come with experience and practice.

As described above, the principle for moving a bullet along a path is:
1. choose a start position - the location of your gun is probably the best choice
2. calculate the direction you want the bullet to travel from that start position - the direction is the coordinate of the mouse pointer minus the start position.
Note: steps 1 and 2 above are done once for each bullet, at the time the gun is fired.
3. after the start position and direction have been determined, then, each time you render the bullet, calculate the current position of the bullet by moving it a small amount from the start position along the direction of flight.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thanks for not giving up on me, you see I've done the basics for a long time actually, but I sometimes have trouble with understanding some weird words that aren't translatable unsure.gif
I will try until I get it working tho =)
-Zaerdna

Thanks for not giving up on me, you see I've done the basics for a long time actually, but I sometimes have trouble with understanding some weird words that aren't translatable unsure.gif
I will try until I get it working tho =)

I sometimes forget that not everyone's first language is English. My apologies for that. :wacko:

Give it a try and, if you can't get it working, post again.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

What words do you have problems understanding ? This is just a general suggestion (for other posters, too): Sometimes it's hard to tell how much a poster already understands, even after several posts. So tell us what's still too cryptic (untranslatable?) for you, so we can adjust our [strike]language[/strike] wording.

(@Buckeye, no offense intended, your posts are always very clear and to the point IMO, ... and ninja'd)

This topic is closed to new replies.

Advertisement