[Pharo-users] Calculate angle between two vectors (probably @Igor :)
nicolaihess at web.de
Mon Mar 17 05:17:02 EDT 2014
Another observation in
ownDirection := self velocity - self position.
the own direction should be the same as the velocity.
Consider a Boid at position 100 at 0 with velocity 100 at 0 would give
ownDirection = 0 at 0.
But of course, the steering direction is still 1 at 0.
2014-03-17 10:07 GMT+01:00 Nicolai Hess <nicolaihess at web.de>:
> This was of course wrong, I don't know what the
> other method computes, but not the angle between two vectors.
> Now the question is, why do you think your method is wrong / the boids
> behave wrong?
> 2014-03-16 18:41 GMT+01:00 Nicolai Hess <nicolaihess at web.de>:
> Rounding error?
>> This may be the reason, igor's version is working with
>> cos = (a dot b)^2 / (|ab|^2)
>> while you are using
>> cos = (a dot b) / |ab|
>> 2014-03-16 17:01 GMT+01:00 MartinW <wm at fastmail.fm>:
>>> i probably made some embarassing mistake, but as i cannot find it, i ask
>>> to have a look at this method.
>>> It should calculate the angle between two vectors. And vectors are
>>> of Point (perhaps here is already a misconception?).
>>> It is used in my flocking simulation PharoBoids
>>> Here is my version of the method:
>>> angleBetween: vector1 and: vector2 onError: aBlock
>>> | cosinusOfAngle innerProductOfVectors productOfVectorsLengths|
>>> innerProductOfVectors := (vector1 dotProduct: vector2).
>>> productOfVectorsLengths := (vector1 r) * (vector2 r).
>>> productOfVectorsLengths = 0 ifTrue: [ ^ aBlock value ].
>>> cosinusOfAngle := innerProductOfVectors /
>>> ^ cosinusOfAngle arcCos
>>> But my Boids behave wrong when i use it. I found another implementation
>>> the same problem by Igor Stasenko which works and looks like this:
>>> angleBetween: p1 and: p2 ifDegenerate: aBlock
>>> " Calculate an angle (in radians) between two vectors.
>>> Evaluate a block, in case if calculation not possible because one of the
>>> vectors has zero length "
>>> | x1 y1 x2 y2 dot2 n2 |
>>> x1 := p1 x.
>>> y1 := p1 y.
>>> x2 := p2 x.
>>> y2 := p2 y.
>>> dot2 := x1 * x2 + (y1 * y2).
>>> dot2 := dot2 * dot2.
>>> n2 := (x1*x1 + (y1*y1)) * (x2*x2 + (y2*y2)).
>>> n2 = 0 ifTrue: [ ^ aBlock value ].
>>> ^ (dot2 / n2) arcCos
>>> Can anybody explain the problem of my method? M.
>>> View this message in context:
>>> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Pharo-users