Sign in to follow this  

Why doesn't this work?

This topic is 3553 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[i] = dist(_camera.Position, v[i]);

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

            for (int i = 0; i < SIZE-1; i++)
            {
                if (d[i] < 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
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

This topic is 3553 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this