• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] I hate vector mathematics!

Status
Not open for further replies.
JASS:
public method projectToPoint (real x, real y, real z, real speed)
        {
            real dx = x - this.xP,
                 dy = y - this.yP,
                 dz = z - this.zP,
                 len = SquareRoot(dx * dx + dy * dy + dz * dz);
            
            this.xV = speed * dx/len;
            this.yV = speed * dy/len;
            this.zV = speed * dz/len;
        }

Here we have a nice bit of Zinc code. It attempts to project a missile towards a point by setting its velocity to the right values. One problem: it seems to randomly not work sometimes. Any ideas where I'm going wrong with the calculations?

PS: Yes, I'm sure the missile moves based on the velocity correctly, and xP/yP/zP are all set to valid values.
 
Nope, commented out all the z stuff and now it just doesn't work right in less dimensions. The exact circumstances are that I have a function to make it follow a unit by using this function to make the missile travel in the direction of the target. It works to follow one unit, but then I change the target and poof. The velocity remains at [0, 0, 0] no matter where the unit goes or what speed I use.
 
Level 9
Joined
Aug 21, 2008
Messages
533
hmm well that means that HERE shouldn't a mistake. I am using something very similar to this for my missles, and I never had a bug.
That you get [0,0,0] may be a clue that something which gives data to the function isnt working like it should.

edit: i remembered that i once had a bug that i just got a very small speed, maybe you get it just 100 times smaller or something
 
Level 20
Joined
Apr 22, 2007
Messages
1,960
Two things:
- Remember to scale your speed value based on the period of your update function. That way, speed will be the same regardless of if you change the period of your function. Otherwise, that wouldn't be the case.

- You should add 0.01 to len. It's possible that you inadvertently set dx, dy and dz equal to 0. It's good practice for this sort of thing.

Yay vector math! Need any more help and just ask me.
 
Status
Not open for further replies.
Top