Cos and Sin math problem (custom 8 direction)
Okay, well I'm still trying to fix my 8 direction movement.
I guess I'm not understanding the Cos and Sin math right? The problem is that the character appears to be moving at angles faster than up, down, right, left. Here's what I have going on:
Repeat While Moved Right
* X( "Player Movement" )+Player Speed*Cos(0)
* Y( "Player Movement" )-Player Speed*Sin(0)
And so on for each direction (replacing 0 with the appropriate angles.
What am I doing wrong? Yes, I know I'm not good at math :P
Re: Cos and Sin math problem (custom 8 direction)
To clarify "Player Movement" is the player object, and Player Speed is a global value for adjusting the speed of the player (obviously).
Re: Cos and Sin math problem (custom 8 direction)
Shawn,
I'm not entirely sure I understand what you are trying to do, but I will upload a screen from a game I'm working on called Asteroids (perhaps later to be called ROIDS. I've got a space ship that you can accelerate in the direction you are pointing and there are counters that calculate the x and y acceleration, velocity, and distance. It's a little cluttered since I've been playing with a number of other possible options, but it might help you get what you want to do done.
Steve
Re: Cos and Sin math problem (custom 8 direction)
Quote:
Originally Posted by drnebula
Shawn,
I'm not entirely sure I understand what you are trying to do, but I will upload a screen from a game I'm working on called Asteroids (perhaps later to be called ROIDS. I've got a space ship that you can accelerate in the direction you are pointing and there are counters that calculate the x and y acceleration, velocity, and distance. It's a little cluttered since I've been playing with a number of other possible options, but it might help you get what you want to do done.
Steve
Well, what I'm just trying to create a basic 8 direction movement. Similar to the built in one, but just so I can customize it more.
But here's what I have so far, in a better explanation:
I have:
Repeat while holding left:
Set X coordinate of Player to Player's X Coord + Speed * Cos(180)
Repeat while holding right:
Set X coordinate of Player to Player's X Coord + Speed * Cos(0)
Repeat while holding Up
Set Y Coordinate of Player to Player's Y Coord - Speed * Sin(90)
Repeate while holding Down
Set Y Coordinate of Player to Player's Y Coord - Speed * Sin(270)
These events let me move in 8 directions, but it's not calculating the angles right and I don't know how to do that. Unless I'm hallucinating, it seems like when I move in the angled directions, you move just a bit faster. I tried solving this a while back but got stuck.
I tried adding the 45 degree angles for up+right etc. but that just created even more problems.
Re: Cos and Sin math problem (custom 8 direction)
Well i've been reading about vectors and things online, and I read something about how I'm supposed to be using radians and not degrees with cos and sin? I have no idea how to do that in mmf.
Re: Cos and Sin math problem (custom 8 direction)
The sin and cos funtions in MMF work just fine with degrees. No need to mess around with radians.
Steve
Re: Cos and Sin math problem (custom 8 direction)
I don't have time to look at this right now, but for the Repeat while holding left, it would be: SEt X coordinate of Palyer to Players X Coord + Speed (no Cos(180) needed). For the four up, down, right, and left, just take out the sin or cos. They are not necessary. You only need the sin(45) when you are moring in both directions simultaneously.
Steve
Re: Cos and Sin math problem (custom 8 direction)
Okay cool thanks! I'll start messing around with it when I get some time and see what happens lol.
Re: Cos and Sin math problem (custom 8 direction)
I put an example of a home made 8 direction motion compared to MMFs 8 direction motion and they are pretty much identical.
Now, if you push the right and up buttons, the object will go both right and up at the same speed, which is not what would happen in the "real" world. If that is what you want, then you would need to change the speed in all of these directions to sin(45)=cos(45)=0.707*speed.
Hope this helps.
Steve
Re: Cos and Sin math problem (custom 8 direction)
Yes, I'm trying to get the object to not travel faster when going in angles, which means I'm going to have to multiply by 0.707.
However, I need some clarification of this line:
"sin(45)=cos(45)=0.707*speed"
I tried multiplying the speed by 0.707 for the angled directions and it produced weird results.
At this point, I have the 8 directions working, I just need the vector movement right, which is where the 0.707 comes into play. I'm apparently not putting the expression in correctly in MMF.
Re: Cos and Sin math problem (custom 8 direction)
I've added an example in the file download section under widgets. There are two ships, one with the code that you have looked at and the 2nd with the code for multiplying the diagonals by 0.707.
You can't just add the multiply by 0.707 directly because then there would be two events happening at the same time when you push the up and right buttons at the same time. One line of code would say do right and another would say do up and right.
So the code gets more complicated - when you push a single button you have to negate the pushing of the other buttons at the same time.
You can see the code - its in the behavioirs inside of the active object. You can find it at:
http://www.clickteam.com/epicenter/ubbthreads.php?ubb=showflat&Number=133345#Post1333 45
Steve
Re: Cos and Sin math problem (custom 8 direction)
Okay, I tried using the widget, it does work. I coded my object using the same events and when placing the widget on top of my object, they move in sync, so they both are coded the same now.
However, the movement in the widget doesn't appear to be correct. Allow me to explain.
I placed the 2 widgets on top of each other, and even an active with built in 8 direction movement on top of those.
The Home Made 8 direction movement moved in the right angles, but moved faster when going in angled directions because it didn't have the adjustment of 0.707. That's what I originally had and was trying to figure out how to correct. Note that the built in movement doesn't have this problem. But this is how the movement works because there is no adjusting for the angles, so that is not a problem with how you did your events, just the math of how the movement works.
The widget with the 0.707 adjustment to correct this seems to have some problems of it's own, however.
For some reason, when moving Down+Right it moves visibly slower than the other angles. What is really strange is that Up+Left seems to move just as fast as the one without any speed adjustment. Also, when moving Down+Left or Up+Right, the widget with 0.707 adjustment didn't move in a 45 degree angle it was slightly off.
Sorry, I'm not trying to be rude and pick apart your widget, it just appears that it is incorrect and I'm trying to figure out why.
The only reason I'm not using built in 8 direction is because I want to have more control over how the movement of the object behaves, but right now I'm having big problems getting it to even just move in the correct angles at the correct speed. And again, please note this happens with your widgets as well, not just my object that I made to match your widget movement, so it's not something I forget to put into my events.
I'm about to the point where I'll just give up and use the built in movement.
I'm not sure if it's a math rounding error in MMF2 or what the problem is. But to see what I mean, set all the objects on top of each other and adjust their speeds to match, and you will see the discrepancy in how they move.
Basically, I want the 8 direction movement at the same speed regardless of direction and moving in the 45 degree angle increments. So far, I can't get that to work.
Edit - if someone else would be kind and test out this widget as well, I'd appreciate it. Maybe we can get to the bottom of this.
Re: Cos and Sin math problem (custom 8 direction)
Shawn,
What you say seems to be correct, but I see no reason why there should be a difference. I tried coding sin(45) instead of 0.707, thinking that there was a round off error. But I got the same result. I'll keep thinking about it - maybe someone with more experience than I can explain what is happening.
Steve
Re: Cos and Sin math problem (custom 8 direction)
I simplified the code to remove as much as possible. I took out all of the animation and made each direction have the same graphic with the same x and y point. I removed all of the buttons except for the ones that go diagonally - 7 = up and left for example. The problem still persists. If you toggle back and forth between the 7 and 3 directions, the ship goes at 45 degrees and stays in place. If you toggle back and forth between the 9 and 1 buttons, the ship does not go at 45 degrees and slowly moves up and to the right. The code is so simple at this point, it would seem to be some kid of bug.
Steve
Re: Cos and Sin math problem (custom 8 direction)
Quote:
Originally Posted by drnebula
I simplified the code to remove as much as possible. I took out all of the animation and made each direction have the same graphic with the same x and y point. I removed all of the buttons except for the ones that go diagonally - 7 = up and left for example. The problem still persists. If you toggle back and forth between the 7 and 3 directions, the ship goes at 45 degrees and stays in place. If you toggle back and forth between the 9 and 1 buttons, the ship does not go at 45 degrees and slowly moves up and to the right. The code is so simple at this point, it would seem to be some kid of bug.
Steve
That's what I was thinking. I don't think it's anything wrong with the events. I think there is actually something buggy with how the movement is being calculated by MMF.
I don't know, maybe Yves can look at it?
Re: Cos and Sin math problem (custom 8 direction)
are you guys sure that the application doesn't register some movements twice or something? Like if you press up&right it registers "up+right" and "up" resulting it going more up than right. If it's something like that, you could put something like
+pressed up
+not pressed left
+not pressed right
-Set Y Coordinate of Player to Player's Y Coord - Speed*Sin(90)
etc.
...ranging out the possibility, it accidentally plays two events the same time. I don't know if it'll work, but just an idea.
Re: Cos and Sin math problem (custom 8 direction)
Quote:
Originally Posted by Tomssuli
are you guys sure that the application doesn't register some movements twice or something? Like if you press up&right it registers "up+right" and "up" resulting it going more up than right. If it's something like that, you could put something like
+pressed up
+not pressed left
+not pressed right
-Set Y Coordinate of Player to Player's Y Coord - Speed*Sin(90)
etc.
...ranging out the possibility, it accidentally plays two events the same time. I don't know if it'll work, but just an idea.
Yep, we already have that in the events, pretty much exactly what you posted. So, I have no clue what is going on.
Re: Cos and Sin math problem (custom 8 direction)
Look at the latest example that I uploaded. In that example, there is only one button to go up and right, one button to go down and right, etc. I don't see how that can be duplicating the motions.
Steve
Re: Cos and Sin math problem (custom 8 direction)
I don't normally like to "bump" posts, but seeing how this is a problem that was never solved, can someone investigate this? Check out the example files in the links above and see what I'm talking about.
In the meantime I just went back to built in movement, but surely you can get the correct angles and speed with Cos and Sin in MMF2? Both drnebula and I cannot figure out why this didn't work.
Re: Cos and Sin math problem (custom 8 direction)
Problem solved!
filearchive post
...stupid me for not thinking about that earlier.