Topic: Rotation formula

Me and SilverstepP have been working on a rotation formula for setting the angle of an object.  We've run into an issue though; we can't get it to work from anywhere but the top-left quadrent in relation to the player.

Here's our formula:

let "$p_xpos_snapshot = player_xpos()"
let "$p_ypos_snapshot = player_ypos()"

//determining squared sides
let "$x2 = ($p_xpos_snapshot - xpos())^2"
let "$y2 = ($p_ypos_snapshot - ypos())^2"

//therefore
let "$h = sqrt($x2 + $y2)" //hypotenuse
let "$x = $p_xpos_snapshot - xpos()" //x value
let "$y = $$p_ypos_snapshot - ypos()" //y value

//from there we must determine the angle and direction to get rotation right
let "$top_angle = rad2deg(asin($x/$h)-pi()/2)"
set_angle "$top_angle"

This works every time from the top left, but never from any other direction.  I'm lost at this point, if anyone know's what's going on please post.

thanks,
-lunarrush

Last edited by lunarrush (2011-07-16 18:51:21)

If I knew then what I know now I'd tell you that the story's true.  Cause whatever you do, it comes back to you.  -Slaughter, Burning Bridges

Re: Rotation formula

This is basically what we need.
http://i826.photobucket.com/albums/zz187/SilverstepP/red_lines.png
The red lines is what we want to determine. We can't find the correct formula for this. We're attempting to make it so that a missile or equivalent projectile will travel towards the character, rotated as if aiming for them, and doing it without completely tracking them (it won't follow you, it moves in a straight line towards you).

In other words, we want to create an object which evaluates the player's position to it in real time and sets it's angle of rotation based on this position.

We're pretty much stumped at this point about why set_angle refuses to rotate it correctly from anywhere but the top left. Is this a bug? Are we doing it wrong? The missile starts with 0 degrees looking as such:

------->

Any help would be appreciated.

Re: Rotation formula

I believe you can solve this using some vector math. Take a vector pointing from the projectile to the player and use the atan2() function. There's a wikipedia article on that.

Re: Rotation formula

Alexandre wrote:

I believe you can solve this using some vector math. Take a vector pointing from the projectile to the player and use the atan2() function. There's a wikipedia article on that.

Ok, I tried using the function like this: let "$top_angle = rad2deg(atan2($y,$x)" and didn't get any rotation.  Am I doing something wrong?

If I knew then what I know now I'd tell you that the story's true.  Cause whatever you do, it comes back to you.  -Slaughter, Burning Bridges

Re: Rotation formula

lunarrush wrote:
Alexandre wrote:

I believe you can solve this using some vector math. Take a vector pointing from the projectile to the player and use the atan2() function. There's a wikipedia article on that.

Ok, I tried using the function like this: let "$top_angle = rad2deg(atan2($y,$x)" and didn't get any rotation.  Am I doing something wrong?

I suggest that you review your math. Print the angle to the screen and figure out what is wrong. The following working code will rotate an object at a rate of 90 degrees per second:

object rotatingInterrogation
{
    requires 0.2.0
    always_active

    state main
    {
        set_angle angle()+90*dt()
    }
}

Please take some time to draw a few vectorss on a piece of paper. I didn't try to solve this particular problem, but I suspect that the atan2 function may be useful: http://en.wikipedia.org/wiki/Atan2 smile

Re: Rotation formula

Ok, but as a note to anyone following this thread it may take me some time.  I've never even touched vector math before, so it will be interesting to see if I can solve this.

Last edited by lunarrush (2011-07-16 22:44:50)

If I knew then what I know now I'd tell you that the story's true.  Cause whatever you do, it comes back to you.  -Slaughter, Burning Bridges

Re: Rotation formula

Alexandre wrote:

I suggest that you review your math.

Does this mean we approached it correctly but had small errors or does this mean we did this completely the wrong way?

Alexandre wrote:

Print the angle to the screen and figure out what is wrong. The following working code will rotate an object at a rate of 90 degrees per second:

object rotatingInterrogation
{
    requires 0.2.0
    always_active

    state main
    {
        set_angle angle()+90*dt()
    }
}

I don't understand how this helps with getting an object to point and rotate towards the player. We're trying to set the angle, not make it spin around...

Re: Rotation formula

The hotspot point from the animation (the .spr file) sets the rotating point of the object.

I dont' know if that helps but I realised it while trying to recode my vent objects.



By the way, There is something I would like to ask you all good math people tongue :

While I was programming the new airflows (now called vents) I tried to do it in a 2 object mode:

The vent and the stop. If the player is between the vent and the stop the player flows.

For the horizontal and vertical vents it was quite easy because it was a simple adding and substracting formula but with the diagonal ones I had many problems so I ended up using a flow instead of a stop so the player flows when touching it.

I tried using vectors and pitagorean trigonometry but all I got was imaginary triangles between the vent and the stop:

http://i1088.photobucket.com/albums/i330/Sugarmaster/object.png

Inverting the formula i got the triangle on the other side.

Is there anyway to get  the object to activate ONLY if the player touches the red line?

I mean only when the player is between the Vent and the Stop in a diagonal line between them.

"The true master is always learning"
Avatar by Raul Sama.

Re: Rotation formula

lunarrush wrote:

Ok, but as a note to anyone following this thread it may take me some time.  I've never even touched vector math before, so it will be interesting to see if I can solve this.

Good. smile

SilverstepP wrote:

I don't understand how this helps with getting an object to point and rotate towards the player. We're trying to set the angle, not make it spin around...

I was saying that the set_angle is ok, but the math isn't. The exact math reasoning you are going to apply is up to you. smile

Sugarmaster wrote:

I tried using vectors and pitagorean trigonometry but all I got was imaginary triangles between the vent and the stop:

http://i1088.photobucket.com/albums/i330/Sugarmaster/object.png

Inverting the formula i got the triangle on the other side.

Is there anyway to get  the object to activate ONLY if the player touches the red line?

I mean only when the player is between the Vent and the Stop in a diagonal line between them.

Having the two points, you can find out the line equation between them. Write it as r: ax + by + c = 0. Now you may use the formula that gives you the distance between a line and a point p=(x0,y0). If I remember correctly, the formula is:

d(r,p) = |a x0 + b y0 + c| / sqrt(a^2 + b^2)

(I suggest doing some research about it)

Now, if d(r,p), the distance between the line and the player, is below some fixed value (like 5 or 10), then you enable the vent. Remember to also check if the player is inside the "vent" x "stop" rectangle (a simple bounding box method will work)

10

Re: Rotation formula

@ Sugarmaster:

In your case, the simplest approach is probably using an invisible object, for which the animation is a triangle with the needed angle.

https://image.ibb.co/kuSYrm/SD_sml.pnghttps://image.ibb.co/kHq8P6/SeD_sml.pnghttps://image.ibb.co/cJf8P6/LTot_W_sml.png