Game Development Community

Magnetism

by Jeff Trier · in Torque Game Engine · 01/29/2004 (6:31 pm) · 5 replies

Hey all,

How can I get a player object to be "pulled/pushed" to a vector location without using a Trigger or PhysicalZone? The reasons I don't want to use either Triggers or PZ's are...

1) I don't want to pull more than the desired object.
2) The magnetic area will be dynamic in location. That is, they will be created and destroyed at various locations regularly.

I am guessing that I need some vector math for this. Now mind you, I am very new at vector math so don't throw eggs when I show you what I have tried... :)

%obj = %client.player;
%dif = vectorSub(%magnetLoc,getWords(%obj.getTransform(),0,2));
%obj.setVelocity(%dif);

My idea here was to get the difference between the player's location and the magnets location, then use setVelocity to propel the player object to the magnets center. Since the above didn't work, I have a feeling that I am way off... duh! lol

Any help would be great.

Thanks!
-Jeff

About the author

Originally a Classical/Metal musician, I've always been attracted to anything involving computers, including: Networking, PC Building and Repair, software design and coding. I've been involved with game design and development for over 10 years.


#1
01/30/2004 (2:31 pm)
Your vector is right for pointing from the target to the magnet, but I think the problem is the inherit velocity of that particular vector, since it can be almost anything. You should normalize the vector (effectively removing any velocity) and then scale the vector. You will have to play around with the amount of the scaling to find what works best. If you have targets of varrying mass, or "material design" you may want to add mass or some other variable from the datablock to determine how attractive the magnet is.

Also, depending on the target you will probably find that when it is dragged along the ground, that the terrain interferes with the movement, which would require that you seperate the members of the scaled vector so you can add a lil extra oomph to the z to bump the target into the air a bit (Provided you have terrain in the first place).

%nDif = vectorNormalize(%dif);
%sDif = vectorScale(%nDif, 20);
%vel = getwords(%sDif, 0, 1) SPC getWord(%sDif, 2) + 3.75;
#2
01/30/2004 (5:03 pm)
Thanks a lot Martin.

I got it to work using vectorScale and vectorNormalize (I wondered what that did!). I am glad I wasn't too far off... Yay! :)

Thanks again,
-Jeff
#3
02/02/2004 (11:02 am)
Btw...

Off the top of anyones head, how would 1 meter equate into world coordinates. Say for example I started at 0 0 0 and ran up the 'Y' axis for 1 meter, what would the difference be?

Thanks guys,
-Jeff
#4
02/02/2004 (4:23 pm)
1 is 1. Meaning that 1 meter in Torque is about what it looks like. In reality, measurement can be any scale you choose. If it looks like a mile, and you say its a meter, then its a meter. Thats the beauty of programming, lol.


About your Trigs or PZ's. What I would do is create a new armor variable called ".canAttract = true;" or ".magnetic = true;" or something like that. When you do the area check around your magnet, just check whatever objects you find to see - if(%obj.canAttract){then pull it;} else{}

You could also use the

vectorDistance(player.getPosition(), magnet.getPosition());

function to get your distance as well. Since magnets are omnidirectional, finding the rotation of both the player and the magnet seems wasteful(if you have a need, then let me hush) And then you'll need to apply an impulse to the player to push him in the correct direction. Since you can apply the ".canAttract" to any object to any object you choose, you will be able to magnatize exactly what you want, and all others will be ignored


Hope that was what you were asking about, I'm a little slow tonight, lol
#5
02/02/2004 (4:38 pm)
Hi Kenneth,

I didn't know about vectorDistance, thanks for pointing that function out. :)

As far as scaling goes, I see what you mean. But for simplicity's sake, I want to keep to Torque's scale of measurement. I am assuming that since the playerData's "maxForwardSpeed" was set in "meters per second", there must be an indication of how long a meter actually is in vectors. Knowing this would be very helpful.

I think I am going to have to wait until I get home and do a series of tests.

Thanks for the reply!
-Jeff