Magnitude Function
yaywaffles95
Member Posts: 12
Hello, I need some advice on how to use the magnitude function. I'm trying to give a specific "attack range" to each character in which they stop moving and instead deal damage to the enemies. Once all enemies are out of range the actor will continue to move forward again. (The game I'm making is similar to Cartoon Wars, Battle Cats, Trenches, Etc.) All help is appreciated!
Answers
If you have one actor at 100, 240 and another at 25, 30, you can find the distance between them with magnitude(25-100,30-240). Cookbook
As behavior:
if magnitude(25-100,30-240) >= 100
-> Move
else
-> Stop
endif
There is a video tutorial here if you want to check it out:
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
On a side note:
I can't imagine why it might be useful . . . . but you can make your own magnitude function by measuring the horizontal distance between two objects, and the vertical distance distance between these two objects, then squaring each of these values, adding the result together and finding the square root of that result, which should look something like this . . .
sqrt(pow((actor1x - actor2x),2)+pow((actor1y - actor2y),2))
. . . which is how I image the magnitude function works.
I got it to work... But only to an extent. If I allow the "enemy" to spawn first, the mechanic seems to work perfectly. However, if I spawn the "friendly character" (which uses a button to spawn) first, it seems to mess up for some reason. @Socks I'm not quite sure why squaring it would be necessary, or is that my problem?
Are you using the method I mention above ? The above method relies on the two measurements mentioned being squared, the maths doesn't work if they are not squared.
[IMG]http://i58.tinypic.com/2s5yxoo.png[/IMG]
This is what I did
@Socks I followed what you wrote, but now the actors don't move at all.
I've not suggested that you actually use my alternative method ! It works just fine, but it clearly says 'on a side note' and that I cannot see why it might be useful to anyone, personally I'd stick with the magnitude function.
@Socks Would it be ok if you went through that alternate method with me on Skype or something? I did it how it was written but the actors don't move at all.
Like I say, I'd personally just use magnitude, the alternative method I posted was simply an aside, I was not suggesting you should use it . . . but if you really wanted to use that method - for whatever reason - then I can guarantee you that it works just fine (just like magnitude works just fine).
I'm still receiving problems, could it be from the fact that there are multiple of the same actors?
...
I'm not quite sure what you meant by that. Did I ask a dumb question or is that why I'm having the issues?
I posted something, but decided it wouldn't be that helpful, so I deleted the post, I was pointing out that you are unlikely to receive a definite answer as your question is pretty vague - or at least your description of the issue.
Information/feedback like "I'm still receiving problems" - "I got it to work... But only to an extent" - "it seems to mess up for some reason" . . . and so on, isn't really that helpful when it comes to working something like this out.
You're asking if your issue is caused by having multiple actors, but there are literally thousands of differing ways you could have multiple actors set up, with the number and variations of rules and attributes and behaviours and conditions and expressions (etc etc) probably running into the many millions, it would be pretty much impossible to work out a definitive answer or suggest what you should do, not only because your project is impossible to reverse engineer from a few clues, but also because the problem we would be looking to resolve with this project we would need to reverse engineer in our minds would be that it 'messes up' or that 'it only works to an extent' which could mean anything . . .
I hope that makes sense !
tl;dr You need to provide much more detailed information
@Socks I have a button that when pressed spawns "actor1". "actor2" is spawned through a timer that I put on the background actor.
actor1: I constrained game.Hero X and game.Hero Y to self.position.x and self.position.y, then game.distance from enemy to round(magnitude(game.Enemy X-self.position.x,game.Enemy Y-self.position.y). If game.distance from enemy is <= 50, then move=0.
actor2: I constrained game.Enemy X and game.Enemy Y to self.position.x and self.position.y, then game.distance from hero to round(magnitude(game.Hero X-self.position.x,game.Hero Y-self.position.y). If game.distance from hero is <= 50, then move=0.
You seem to be constraining game.distance to the distance between the Enemy and the Hero . . . . twice ?
Obviously with any 2 objects, let's call them A and B, the distance between A and B is the same as the distance between B and A (axiomatically) so there would be no need to duplicate the measurement.
Also I don't really know what 'move=0' means, it could mean a whole bunch of things (why not simply say what the actual rule is and the actual values it uses ?) . . . and I'm still not entirely sure what the issue is, you've still not actually said, you've only alluded to 'still receiving problems' (and so on), it would be helpful to know what the actual issue is.
@Socks You seem to know what you are doing so would you like to take a look at it? It would just be too complicated to explain what's going on.
No ! Not at all ! Lol
Your point that it is 'too complicated' says all I need to know, I'm not a fan of spending 2 hours looking through someone else's project I am not familiar with . . . . if you have any specific questions I'd be happy to answer them (as will a whole bunch of other people here on the forums) . . . if you want to attract people to your question you'd need to clearly state the issue - which is still a mystery after several posts, it could be that one of the actors doesn't stop, or one stops but shakes, or one stops but not at the right distance, or the expression you are using gives you an 'invalid expression' error . . or a thousand others things.
For all anyone knows the problem could be with the 'move=0' rule, which is also a mystery, 'move=0' could mean lots of different things - it'd seem obvious enough to me to show people the rules you have - a screenshot would be ideal or if you don't want to do that then at least a clear explanation of what things like 'move=0' actually are . . . but anyway the general point I am making is that with clear unambiguous questions get answers very quickly, vague questions often don't get answers at all . . . when people post screenshots of the rules they are having issues with and even a little scribble drawing of what should happen or a video of the project showing the issue they will usually get a few decent replies.
@Socks I finally figured it out! I was spawning the "enemies" using the background and relative to the background. This made the actors randomly stop, walk past others, attack nothing, etc. The solution was to make a separate spawner that the "enemies" spawn within. Thank you so much for your help everyone! I appreciate it! (especially Socks)
Not at all, you actually worked it out all by yourself ! Lol.
Good luck with the project !
Thanks!