Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charge.point not updated when moving it #22

Open
Viicos opened this issue May 31, 2022 · 0 comments
Open

Charge.point not updated when moving it #22

Viicos opened this issue May 31, 2022 · 0 comments

Comments

@Viicos
Copy link
Contributor

Viicos commented May 31, 2022

So whenever a Charge object is instanciated, the attribute point is set:

class Charge(VGroup):
def __init__(
self,
magnitude: float = 1,
point: np.ndarray = ORIGIN,
add_glow: bool = True,
**kwargs,
) -> None:
"""An electrostatic charge object. Commonly used for :class:`~ElectricField`.
Parameters
----------
magnitude
The strength of the electrostatic charge.
point
The position of the charge.
add_glow
Whether to add a glowing effect. Adds rings of
varying opacities to simulate glowing effect.
kwargs
Additional parameters to be passed to ``VGroup``.
"""
VGroup.__init__(self, **kwargs)
self.magnitude = magnitude
self.point = point

but this attribute is not updated when using methods such as move_to or shift:

class Sample(Scene):
    def construct(self):
        charge = Charge()
        print(charge.point)
        charge.shift(UP)
        print(charge.point)
>>[0. 0. 0.]
>>[0. 0. 0.]

This implies some incorrect behaviour e.g. when using ElectricField.get_force_on_charge:

class Sample(Scene):
    def construct(self):
        charge_1 = Charge(add_glow=False)
        charge_2 = Charge(add_glow=False).shift(UP)
        field = ElectricField(charge_1)
        f = field.get_force_on_charge(charge_2)
        self.add(charge_1, charge_2, field, f)
Result

Sample_ManimCE_v0 15 2

You can see here that the f vector is still located on the origin, where the charge_2 was previously located.

This happens because the code of the get_force_on_charge method is using the charge.point attribute L147:

def get_force_on_charge(self, charge: Charge, **kwargs) -> Vector:
"""Returns a vector corresponding to the force
of the electric field on the charge.
"""
p = charge.get_center()
direction = np.zeros(3)
for other_charge in self.charges:
if other_charge == charge:
continue
p0, mag = other_charge.get_center(), other_charge.magnitude
x, y, z = p - p0
dist = np.linalg.norm(p - p0) ** 3
if (x**2) > 0.01 or (y**2) > 0.01:
direction += mag * np.array([x / dist, y / dist, 0])
else:
direction += np.zeros(3)
length = (direction[0] ** 2 + direction[1] ** 2) ** 0.5
vec_start = (
Vector(direction / length * charge.radius).shift(charge.point).get_end()
)
return Vector(direction, **kwargs).shift(vec_start)

So maybe to fix this the methods moving the object such as shift, move_to, to_corner should be overrided? The thing is we can't be 100% sure that all the methods modifying the object's position have been taken into account. Maybe there's a more general way of fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant