Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Trigonometry Tango! (https://forums.graalonline.com/forums/showthread.php?t=58058)

Deek2 03-14-2005 02:13 AM

Trigonometry Tango!
 
1 Attachment(s)
Hello,
I have recently been interested in developing a script that uses an attractive gravitational force that spins nearby players towards yourself in a vortex spiral direction. I am merely interested in the mathematical concepts/equations relevant to achieve this. I would prefer to work out the nuts and bolts by myself, since I am inexperienced in the gscript language.
Oh, woe! If only I weren't mathematically inept (I haven't even started Geometry yet!).
Observe this visual aid:

Evil_Trunks 03-14-2005 02:32 AM

if such a thing existed on classic we would probably deem it a "spiral vortex" ;)

im having a hard time breaking down the math it uses, it doesn't do a "realistic" pull however

Kaimetsu 03-14-2005 02:37 AM

Depends on the nature of the pull. You'll need to calculate some kind of force to be exerted on the objects, probably proportional to the object's mass and inversely proportional to the square of the distance to the centre. Then use a simple F=ma to determine the acceleration and add it to the object's velocity.

Again and again and again.

Deek2 03-14-2005 02:52 AM

Quote:

Originally Posted by Kaimetsu
Depends on the nature of the pull. You'll need to calculate some kind of force to be exerted on the objects, probably proportional to the object's mass and inversely proportional to the square of the distance to the centre. Then use a simple F=ma to determine the acceleration and add it to the object's velocity.

Again and again and again.

I'm not quite sure how one would represent a player's mass on gscript.

Kaimetsu 03-14-2005 03:01 AM

Quote:

Originally Posted by Deek2
I'm not quite sure how one would represent a player's mass on gscript.

Think of a number. That's the player's mass.

Any units are fine. It just has to be consistent throughout the universe. As long as the ratios are intact (ie, chairs weigh less than houses, etc), you're okay.

Zero Hour 03-14-2005 03:06 AM

Quote:

Originally Posted by Kaimetsu
Think of a number. That's the player's mass.

Any units are fine. It just has to be consistent throughout the universe. As long as the ratios are intact (ie, chairs weigh less than houses, etc), you're okay.

That's a pretty neat idea, would give the vortex some detail [:

Polo 03-14-2005 03:07 AM

As an alternative to the (very correct and very good) force based method Kai is suggesting, we can create 'pretty much' the same effect in a different way.

Theres 2 things we need to consider. The angle between the player and the vortex, and the distance away the player is.

We can work out the distance by using Pythagoras Theorem...

√(δx^2 + δy^2) = distance,
where δx is the horizontal difference,
and δy the vertical difference.

Next we need to work out the angle... this can be down with the arctan() function.

arctan(δx/δy) = distance,
where δx is the horizontal difference,
and δy the vertical difference.

Now... this will only give us a result between -pi/2 and pi/2 radians (-90 and +90 degrees), and so the first half and second half of our 'circle' of motion will wield the same result. We can compare whether or not δx and δy are positive or negative, and work out which quadrant (quarter) of the full circle the player is in. We can then add on pi or so to get the correct angle we want for all of the circle...
(Graal does however provides a function to do all the angle calculation for you - I'll leave it up to you to find it.)

Now that we have both the angle and the distance we can do some manipulation...

To create the spiral vortex type motion, we need to add some value to the angle, and decrease the distance. Im sure you can work out how to do that ;).

Now we have to convert the distance and angle back into a new δx, δy. We can turn the angle back into a component x/y by using sin(angle) and cos(angle). the results these give are the ammount of x and ammount of y we would have if the distance were 1... however the distance is not 1. Simple solution.. simply multiply by our newly calculated distance.

new-δx = distance*cos(angle)
new-δy = distance*sin(angle)

You may need to adjust those (ie: add a negative factor to one of them, or switch cos for sin in both... it depends how you got the angle and how you visualize it n stuff), but all the maths is there for you.

Hope you understood all that.

Kaimetsu 03-14-2005 03:17 AM

Quote:

Originally Posted by Polo
As an alternative to the (very correct and very good) force based method Kai is suggesting, we can create 'pretty much' the same effect in a different way

Well, trigonometry works, but it's less efficient and actually more complicated, so I'm not sure if it's advisable. Plus, if you wanted to incorporate other forces (like if the player is trying to move) or produce non-circular orbits, you'd need to maintain independent velocities anyway.

But I guess it's about what's most comfortable and familiar to the scripter, and examining multiple approaches is a good exercise.

Polo 03-14-2005 03:24 AM

Quote:

Originally Posted by Kaimetsu
Well, trigonometry works, but it's less efficient and actually more complicated, so I'm not sure if it's advisable. Plus, if you wanted to incorporate other forces (like if the player is trying to move) or produce non-circular orbits, you'd need to maintain independent velocities anyway.

But I guess it's about what's most comfortable and familiar to the scripter, and examining multiple approaches is a good exercise.

Yeah, you cant get the same kind of accuracy thats true, but then I dont think people would really notice the difference. I decided to post it as (in the UK) you cover pythagoras and trigonometry long before the concept forces (in mathematics) and stuff during you education... so I thought it may seem more 'accessible' to those who have covered less mathematics.

To be fair though, i'd probably do it with forces if I was to go and script it right now.

Deek2 03-14-2005 03:26 AM

What needs to be stated is that players that are farther away from the vortex's epicenter will need to have a greater mass than someone who is closer. Since the vortex's focal point will be my current coordinates, players who are closer to me will hypothetically weigh less than the players that are farther away, hence the gravitational pull and force applied to these players will be much greater. I'm just thinking out loud.
I believe I can find a way to attribute these mass quantities to the relevant players, but I'm still trying to comprehend these math equations, only having the knowledge of algebra and the scripting experience of two weeks, this makes for quite a formidable challenge!

Kaimetsu 03-14-2005 03:36 AM

Quote:

Originally Posted by Deek2
Since the vortex's focal point will be my current coordinates, players who are closer to me will hypothetically weigh less than the players that are farther away, hence the gravitational pull and force applied to these players will be much greater

O_o

It's not possibly to alter the mass of an object without turning it into multiple objects. If you want the force to increase when the player is nearer, simply make it inversely proportional to the square of the distance, as I said before.

Polo 03-14-2005 03:38 AM

Quote:

Originally Posted by Deek2
...players who are closer to me will hypothetically weigh less than the players that are farther away, hence the gravitational pull and force applied to these players will be much greater.

This is what Kai referred to as 'inverse square'. Basically this just means that thos twice as far away, will have half the pull. The formula for calculating this 'theoretical mass' is mass/(distance^2).

Deek2 03-14-2005 05:05 AM

Okay, so I've figured out the distance and the angle
NPC Code:
for (this.p=0;this.p<playerscount;this.p++){
this.distance=((playerx-players[this.p].x)^2 + (playery-players[this.p].y^2))^.5;
this.angle=arctan((playerx-players[this.p].x)/(playery-players[this.p].y^2));
}


Or at least I think.
The difficulty I'm encountering now is how to actually use the variables that I created. I don't know how to even figure out which certain quadrants the players fall into.

Polo 03-14-2005 06:15 AM

The player is eithe above or below, and to the left or right... that 4 quadrants. However, like i said before, Graal has a function for getting angles that will handle this for you.

Python523 03-14-2005 06:56 AM

Quote:

Originally Posted by Polo
This is what Kai referred to as 'inverse square'. Basically this just means that thos twice as far away, will have half the pull.

Twice as far has a quarter the gravitational force


All times are GMT +2. The time now is 03:10 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.