Jump to content
  • Advertisement
Sign in to follow this  
Zaerdna

shoot bullet towards mouse pos

This topic is 2804 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 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.[/quote]
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.

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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)

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!