HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Making a spring with vectors

10-05-2008, 02:56 AM#1
dead_or_alivex
Could anyone outline a method with which I could make a Hooke's-law-compliant (force being directly proportional to extension) 'spring' with Anitarf's Vector System?

I'm capable of making a simple one with 'normal' methods, but I'm a little lost about how to go about doing it with vectors (I'm trying to make it work with other modules, such as unit collisions). Still confused about stuff like whether force should be a vector, or whether acceleration, velocity and/or displacement instead should be vectors, etc.

I guess what I'm looking for is another example, and possibly a way to approach this problem.

About the spring, I eventually intend to assign every unit a mass, so that could be used in calculation. Also, the spring is going to be made with lightning effects, so handling a unit chain is so problem for now.

Slightly off-topic: Is the dot product of two vectors only used to find the angle between them? I read the Wikipedia article and didn't really understand.

Edit: Put reference links in.
10-05-2008, 07:12 AM#2
grim001
Just what we need, another guy making a physics engine...

The concept is pretty simple though. Just take the distance between the two objects on each side of the "spring." If the distance is greater than the length of the spring, pull them together proportionally to how much farther they are apart than they should be. And do the inverse if they are too close together.

You should join IRC if you need help with the vector math parts.
10-06-2008, 07:19 AM#3
Jazradel
I don't think he is. I think he's trying to use Anitarf's.

Force, Acceleration, Velocity and Displacement are all vectors.

Anything which has a magnitude and a direction is a vector.
10-06-2008, 10:48 AM#4
Anitarf
Quote:
Originally Posted by Jazradel
Force, Acceleration, Velocity are all vectors.
Displacement isn't.
Umm, no, they can all be represented with vectors.
10-06-2008, 12:06 PM#5
Belphegor666
Really? In physics they are. Why wouldn't they be represented by a vector in wc3?
10-06-2008, 06:28 PM#6
MaD[Lion]
every grouped quantities can be presented as vector.
["you","me","we","he","she"] is also a vector
10-06-2008, 06:55 PM#7
Rising_Dusk
Quote:
Originally Posted by Jazradel
Force, Acceleration, Velocity are all vectors.
Displacement isn't.
I think you have distance and displacement mixed up.
10-07-2008, 09:43 AM#8
Jazradel
Probably.

I was only talking about Vectors in physics. No idea what you can use Anitarf's system for.
10-07-2008, 11:11 AM#9
dead_or_alivex
^ Displacement is a vector in physics. Anitarf's system, if I'm not wrong, just helps when calculating vectors in 3D space.

Quote:
Just what we need, another guy making a physics engine...
Heh, call it a learning project.

This is what I have so far (not very much). It doesn't work, also; nothing happens:

Collapse JASS:
scope Spring initializer Init
globals
    private timer t
    private lightning spring
    private unit wisp
    private vector s
    private vector f
    private constant real k = 100
    private real defaultx
endglobals

private function HandleSpring takes nothing returns nothing
    local real ext = s.getLength() - defaultx
    
    set s.x = GetUnitX(wisp)
    set s.y = GetUnitY(wisp)
    call MoveLightning(spring, false, 0., 0., s.x, s.y)
    
    call f.setLength(ext * k)
    call s.add(f)
    
    call SetUnitX(wisp, s.x)
    call SetUnitY(wisp, s.y)
    
    call BJDebugMsg(R2S(ext))
endfunction

private function Init takes nothing returns nothing 
    local real x = GetRandomReal(-300., 300.)
    local real y = GetRandomReal(-300., 300.)
    
    set wisp = CreateUnit(Player(0), 'e000', x, y, 0.)
    set spring = AddLightning("AFOD", false, 0., 0., x, y)
    set s = vector.create(x, y, 0.)
    set defaultx = s.getLength()
    set f = vector.create(0., 0., 0.)
    call f.projectVector(s)
    call f.scale(-1.)
    set t = CreateTimer()
    call TimerStart(t, .02, true, function HandleSpring)
endfunction
endscope

I would normally do it with displacement calculated with some proportionality constant, so I'm not sure where I've gone wrong here. Kinda new to the vector approach.

Also, I've only ever been exposed to 2D vectors, so the extra dimension is a bit... tough to grasp, at the moment.
10-07-2008, 12:27 PM#10
Anitarf
Your vector f is 0 units long.

Also, you got it all wrong, force results in acceleration, not displacement.
10-07-2008, 05:18 PM#11
dead_or_alivex
But f gets updated later, in the periodic function. Uh, is that right?

I figured using the magnitudes of force and displacement in F=kx (Hooke's law - force being proportional to extension) would work. I don't know - as I said, I'm quite sketchy on how to approach this.
10-07-2008, 07:36 PM#12
Anitarf
Quote:
Originally Posted by dead_or_alivex
But f gets updated later, in the periodic function. Uh, is that right?
Attempting to set the length of a zero vector (which doesn't have a defined direction) results in failure, as is noted in the documentation.

Quote:
I figured using the magnitudes of force and displacement in F=kx (Hooke's law - force being proportional to extension) would work. I don't know - as I said, I'm quite sketchy on how to approach this.
Force is proportional to extension, yes, but that force then results in the acceleration of the object, it doesn't cause displacement directly, so instead of modifying the unit's position you should be modifying it's velocity.
10-07-2008, 11:13 PM#13
MaD[Lion]
Attempting to set the length of a zero vector (which doesn't have a defined direction) results in failure, as is noted in the documentation.

actually anitarf if u set length of a 0 length vector it should be a bigger dot/sphere. since direction is infinite then, tat means the length will be on all these infinite directions.

haha joking im just tired ignore me.