Jump to content
  • Advertisement
Sign in to follow this  
homer_3

Why doesn't this work?

This topic is 3745 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 trying to make an object come towards me. To do this, I move the item I want to come towards me in all possible directions by 1. Whichever direction moves the object closes to me, I move it in that direction. The object will move in my general direction, but it will just continue on past (not through) me and off into infinity. My code is:
private bool shoot(UnitBase obj)
        {
            const double HIT = 5;
            const int SIZE = 26;
            double[] d = new double[SIZE];
            int smallest = 0;
            Vector3[] v = new Vector3[SIZE];

            v[0] = new Vector3(obj.Position.X+1, obj.Position.Y+1, obj.Position.Z+1);
            v[1] = new Vector3(obj.Position.X, obj.Position.Y+1, obj.Position.Z+1);
            v[2] = new Vector3(obj.Position.X+1, obj.Position.Y, obj.Position.Z+1);
            v[3] = new Vector3(obj.Position.X+1, obj.Position.Y+1, obj.Position.Z);
            v[4] = new Vector3(obj.Position.X, obj.Position.Y, obj.Position.Z+1);
            v[5] = new Vector3(obj.Position.X, obj.Position.Y+1, obj.Position.Z);
            v[6] = new Vector3(obj.Position.X+1, obj.Position.Y, obj.Position.Z);
            v[7] = new Vector3(obj.Position.X-1, obj.Position.Y-1, obj.Position.Z-1);
            v[8] = new Vector3(obj.Position.X, obj.Position.Y-1, obj.Position.Z-1);
            v[9] = new Vector3(obj.Position.X-1, obj.Position.Y, obj.Position.Z-1);
            v[10] = new Vector3(obj.Position.X-1, obj.Position.Y-1, obj.Position.Z);
            v[11] = new Vector3(obj.Position.X, obj.Position.Y, obj.Position.Z-1);
            v[12] = new Vector3(obj.Position.X, obj.Position.Y-1, obj.Position.Z);
            v[13] = new Vector3(obj.Position.X-1, obj.Position.Y, obj.Position.Z);
            v[14] = new Vector3(obj.Position.X+1, obj.Position.Y+1, obj.Position.Z-1);
            v[15] = new Vector3(obj.Position.X+1, obj.Position.Y-1, obj.Position.Z+1);
            v[16] = new Vector3(obj.Position.X-1, obj.Position.Y+1, obj.Position.Z+1);
            v[17] = new Vector3(obj.Position.X+1, obj.Position.Y-1, obj.Position.Z-1);
            v[18] = new Vector3(obj.Position.X-1, obj.Position.Y+1, obj.Position.Z-1);
            v[19] = new Vector3(obj.Position.X-1, obj.Position.Y-1, obj.Position.Z+1);
            v[20] = new Vector3(obj.Position.X+1, obj.Position.Y, obj.Position.Z-1);
            v[21] = new Vector3(obj.Position.X+1, obj.Position.Y-1, obj.Position.Z);
            v[22] = new Vector3(obj.Position.X, obj.Position.Y+1, obj.Position.Z-1);
            v[23] = new Vector3(obj.Position.X, obj.Position.Y-1, obj.Position.Z+1);
            v[24] = new Vector3(obj.Position.X-1, obj.Position.Y+1, obj.Position.Z);
            v[25] = new Vector3(obj.Position.X-1, obj.Position.Y, obj.Position.Z+1);

            for (int i = 0; i < SIZE; i++)
                d = dist(_camera.Position, v);

            for (int i = 0; i < SIZE; i++)
                if (d < HIT)
                    return true;

            for (int i = 0; i < SIZE-1; i++)
            {
                if (d < d[i + 1])
                    smallest = i;
            }
            
            if (d[SIZE-1] < d[smallest])
                smallest = SIZE-1;

            shortest_dist = d[smallest];

            obj.Position = v[smallest];

            return false;
        }

        private double dist(Vector3 player, Vector3 obj)
        {
            return Math.Sqrt(Math.Pow((player.X-obj.X),2) + Math.Pow((player.Y-obj.Y),2) + Math.Pow((player.Z-obj.Z),2));
        }


Share this post


Link to post
Share on other sites
Advertisement
Why don't you just get the direction vector and move along that?

Seriously, it's as simple as this: (pseudocode)

float distanceToMove = 1.0f; // Or whatever you want it to be

Vector3 direction = _camera.Position - obj.Position;
direction.Normalize();
obj.Position += direction * distanceToMove;

Hope this helps.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!