Thread: Firing problem in space sim

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Firing problem in space sim

    Having problems again with the laser firing code.

    I think what I need to do is take the look vector some distance out in front of the ship and subtract it from the position of the weapon to get the right vector.

    Code:
    D3DXVECTOR3 Target=Camera.Look;
    
    //Range of weapon
    Target.z+=(Camera.Look.z*20.0f);
    
    D3DXVECTOR FromWeaponToTarget;
    
    FromWeaponToTarget=Target-Weapon.Position;
    
    D3DXVec3Normalize(&FromWeaponToTarget,&FromWeaponToTarget);
    But how do I rotate the model so that it is always facing the direction it is firing?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    Could you please describe in more detail what you are trying to accomplish and what the problem is. It seems as if this is something that would be easy for you therefore I feel I don't quite understand what is really going on. Otherwise, I have written code to make something look in the direction of something (I had a monster's head look at the player regardless of where the player was, looked like something from the exorcist). It's just a matter of computing pitch and yaw. You just do basic trig to compute the angles, then build the orientation matrix from that. If you want I can post code.

    edit:
    umm, this is the only code I can find that performs these tasks. Note that the 'fast' functions are from when I experimented with implementing my own square roots, it wasn't practical but this code works. It also is with OpenGL right handed coordinate system with negative z into the screen, therefore you adjust as necessary. also the code is terribly inefficient but as i said it does work.
    Code:
    float	FastVecToPitch(Vector3	*Vec)
    {
    	Vector3	copy = *Vec;
    	copy.FastEnsureUnitLength();
    	return	RAD2DEG(asinf(copy.y));
    }
    
    float	FastVecToYaw(Vector3	*Vec,Vector3	*Origin)
    {
    	Vector3	copy = *Vec;
    	if	(copy.y)
    	{
    		copy.y = 0;
    		copy.Fast_Normalize();
    	}
    	else
    	{
    		copy.EnsureUnitLength();
    	}
    
    	if(Origin->y)
    	{
    		Origin->y = 0;
    		Origin->Fast_Normalize();
    	}
    	else
    	{
    		copy.EnsureUnitLength();
    	}
    	return	RAD2DEG(acosf(DotProduct(&copy,Origin)));
    }
    You have to be careful how you use it, i.e, you have to negate the angle from VecToYaw in certain instances.
    Code:
    	if(pTarget->mPosition.x > this->mPosition.x)
    		mYaw = -mYaw;
    I believe to solve this problem, use tan, but check for division by zero.
    Last edited by Darkness; 03-26-2005 at 05:57 PM.
    See you in 13

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm flying through space and I want to shoot a laser in the direction of flight. The laser quad must be oriented to face the direction it is moving.

    All I'm doing is simply inverting the view matrix so that when I specify Vector(0.0f,0.0f,1.0f) this means the laser shoots 'out' regardless of the orientation of the camera. If I invert the view matrix and set that as my world matrix, then I have effectively 'undone' any rotations caused by the camera. So now everything is easy to rotate and easy to use, but I'm doing something wrong because it's not working like I planned.


    I'll figure it out or I'll just erase the whole class and start over.

    In fact I'm going to totally re-do the entire engine because I'm not happy with the current architecture and I'm not happy with the current code base.

    It needs a complete facelift and rather than try to edit existing code, I'm going to change the whole setup.

    But after spending 4 hours messing with MFC and having it totally erase my work because MS can't program anything remotely stable, I'm not really into coding right now. Heck I might just erase the whole frieking XSpace and start over.
    Last edited by VirtualAce; 03-26-2005 at 06:20 PM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    I understand how you feel. I also understand that it's near impossible to get a perfect design or architecture. My policy is if it works and it is fast and the layout is reasonably intelligible then it is okay.

    I see what you are trying to do and I think you'll basically be able to figure it out on your own.
    See you in 13

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well thanks for the vote of confidence but this stuff is frustrating. Some days you code and everything goes as planned and you are like....yeah I'm a 3D god. Then the next day you code and you are wondering whether or not you should even attempt 3D graphics.

    It takes a lot of patience and a lot of persistence to get this stuff working. If I was getting paid for it and had to do this everyday I'd be done by now, but since this is my own project and often times I'm too tired from work to code on it....it is taking forever.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    I don't know about the getting paid part. I have yet to actually hear somebody who enjoys their game programming job. Because you are getting paid, you are under constant intense stress and it seems like it isn't fun. Also, if you were getting paid you would be working on a much more elaborate project, and it'd still take forever. To be fair I've never personally held a game programming/game development job.
    Last edited by Darkness; 03-29-2005 at 07:40 AM.
    See you in 13

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Don't get me wrong though, I WILL fix this problem and it won't beat me. I didn't get this far by whining about it nor by asking others to solve all my problems or dole out code willy nilly. I'll fix it and be the better for it, but at times it is tiresome. However, I'll get it working and move on to the next problem as I'm sure there are many more down the road.

    Maybe threads like these are what's needed on this board. So many people look at me on this forum and think wow he knows a lot and they think I can pretty much do anything in 3D. Truth is, I do know a lot and you do to, but we both know there is so much that we do not know and so much that remains to be discovered that even the best 3D gaming guru out there...still doesn't know it all.

    So I do appreciate the nice comments from all the posters about my game and my screenies but especially for the new people here I'd like to say that don't be fooled. Just because these screenies might look very good to you, does not mean I am anything special or some awesome C++ guru or 3D guru. And if I can do this stuff then you can to. All it takes is passion, desire, commitment, and dedication to the art. Everyone has the capability to learn, but not everyone has the right attitude. Attitude is about 90% of the whole works - most people defeat themselves when it comes to code.

    To be frank, Direct3D is cake. All it amounts to is what function do I need to do this or that and you call it with the right parameters. All the setup and other things are pretty much the same once you get your first system up and running. Once you know the API or at least can use the help file to find the function you need, once you understand the basics of 3D graphics and math, and once you get to the point that C++ is really second nature, the rest is just about problem solving and applying what you have learned.

    So look at this thread and you can see my frustration and my honesty about what I can do. But I'm willing to learn and ...willing to fail miserably and start all over.

    It's not whether or not you fail because I guarantee you will fail at some point, it's how you deal with that failure that separates the winners from the losers.

    And now....back to that doggone class.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    I know you are against having people 'spoon feed' information or whatever, but do you want me to just draw you a god damn picture and explain how to use the code I posted? I'm surprised you don't have this solved already to be honest. It doesn't mean you even have to take my approach, because the problem of calculating the euler angles to align one object to another is a common one, but everybody can use a friggin bump in the right direction every now and again.

    EDIT:
    and the code I was posted was incomplete anyway. I was finding the angles from the negative z axis not the x axis, which is an odd way of computing yaw, but it's how I decided to do it.
    Last edited by Darkness; 03-29-2005 at 12:21 PM.
    See you in 13

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM