# Magnet repulsion

0 favourites
• 10 posts
• Hi. I've follow an example in forum for create a repulsion magnet.

Now I've the player that change gravity force in positive or negative. When the gravity is positive, magnet work fine, but with negative gravity, magnet attracts the player.

Here the image of the events, maybe someone can help me.

<img src="http://i45.tinypic.com/149a261.png" border="0" />

:

Cap file

jumpshare.com/v/lqe0MAZdphluQijRq5Ug

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• It looks like an error with the variable 'forza', although maybe there's more code above this that we can't see. But as this code stands, it looks as though it will toggle the variable every frame. One frame does -forza, then the next frame it does -forza again so it'd keep toggling?

The way I'd do it, I'd move the object towards/away from the magnet, just using the angle between the two, and just set up a seperate line of code for each situation. So....

Animation Frame = 0

Move Object at angle(x,y,x,y) + distance

Animation Frame =1

Move object at angle(x,y,x,y) - distance

That might be a bit too simple for your needs though, it looks like you're trying to do it a bit more precisely.

• perhaps it is as simple as

if gravityReversed > fx *= -1, fy *= -1 right before you add the fx and fy to the vectors at the end?

• : Don't work

cincipon: I don't understand, sorry i'm not english, you can explain it better?

• I'm suggesting that since it works fine when gravity is normal, having a condition that checks to see if gravity is reversed will let you then add an action that reverses the force of the magnets. The best place I see to do this is right before you "Set Platform VectorX" and "Set Platform VectorY".

Something like :

On Condition "GRAVITY IS REVERSED"

Action "fx = fx * -1" Action "fy = fy * -1"

• Don't work. Here my capx file, so you can look it!

jumpshare.com/v/lqe0MAZdphluQijRq5Ug

• Hmm. I see that your problem is more complicated than I thought from your description.

If you want the magnet to repulse, perhaps it would be best tomake an invisible sprite around it, and when the player collides with the trigger sprite you could send it a physics impulse based on the angle between the player and the magnet?

As it stands now, your code has too much going on aside from the problem at hand for me to jump into.

Good luck!

• Hi,

I�ve the same proble... i follow the another post... no have a another "easy solution" for magnet/repulsion objet?

Note: Would be great in future a beahvior with parameter for repulsion or magnet with a time counter... for example if 1 objet "on collision with X" i can put magnet with the time i want for the option magnet... the same with repulsion.

• You are complicating stuff

Just use the physics behavior !

(You could flesh the player controls , it's very basic but it does the job)

There's Repulsion and Attraction depending on the magnetwave angle

Cheers !

• Thanks! I use your tips for my project.

Note:a behavior/plugin with some options would be great ;)

• 10 posts