lightbulb Preferred developing language

More
3 years 4 months ago - 3 years 4 months ago #20554 by IronyMan
Link works for me. :?

Here's a text file with it; paste it all into a new program on Khan.

The controls mimic Starsector; I think they work pretty well. But then, I play Starsector a lot... :unsure:

Hoy, good problem solving! I hope I have as much luck with my issue.
Attachments:

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #20559 by IronyMan
Hang on, new link.

Should work now. Sorry about that.

I've added rotational momentum, and that has further aggravated the issue of mouse control. I think I'll drop that problem for now, but any input is welcome.

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago - 3 years 4 months ago #20561 by Chessking
The link still doesn't work for me. Perhaps something in your account settings does not let other people view your programs? I'm not sure. Perhaps if you give me the link to your profile, rather than a specific program, that will work.

I know what your problem is. Here is the code starting at line 172. I added the rotation variable and the two println() statements for debugging purposes.
if (char.inship === true && control.occupied === true) {
      char.a = control.a;
      var rotation = atan2(mouseY-width/2,mouseX-height/2)-90;
      println(rotation);
      println(control.a);
      if (keys[SHIFT]) {
          if (control.a < atan2(mouseY-width/2,mouseX-height/2)-90) {
              control.a += control.agility;
          }
          
          if (control.a > atan2(mouseY-width/2,mouseX-height/2)-90) {
              control.a -= control.agility;
          }
                
      }
Plug this code into your program and you will see the problem. At the left hand side, the rotation variable switches from 270 to -90, which obviously affects whether control.a is greater than or less than the mouse angle.

To solve this, we need to designate an optimal situation. We can do this in the form of an IPO (input, processing, output) chart. The inputs are the ship rotation, and the mouse rotation to the center of the screen. I think the desired output should be the angle between these two angles. When the ship is facing the mouse, the output should be 0. As the mouse moves clockwise around the ship, the angle should increase, reaching 180 behind the ship. As the mouse moves counter-clockwise around the ship, the angle should decrease, reaching -180 behind the ship. The switching point between positive and negative angles should always be behind the ship.

The final stage is the processing stage. How do we get from input to output? First, we define a variable for each input. We already have control.a defined, so lets add
var rotation = atan2(mouseY-height/2,mouseX-width/2);
for the mouse angle.

Now, let's say the ship is rotated 100 degrees, and is facing the pointer. Both input variables will be the same. If we subtract control.a from rotation, the angle will be 0 whenever the ship is facing the pointer.
var rotation = atan2(mouseY-height/2,mouseX-width/2)-control.a;
Now, not only is rotation 0 when the ship is facing the mouse, but the angle increases as the mouse moves clockwise around the ship, and decreases when the mouse moves counterclockwise around the ship, reaching -180 behind the ship. However, the number continues to decrease beyond that point, until it switches to a positive number at the left.

To fix this, all we have to do is add 360 to var rotation when rotation is less than -180. At the same time, we should subtract 360 when rotation is greater than 180.
if(rotation >= 180) {
    rotation -= 360;
}else if(rotation < -180) {
    rotation += 360;
}
I originally used a while loop instead of an if statement, because the manual rotation of the ship allows control.a to reach values much higher than 180, 360, or even 1000. However, I then discovered this section of code in your program:
if (control.a > 360) {
    control.a = 0;
}
This solves the problem if control.a reaches high values, but not if it reaches low values. So I would suggest changing it to this:
if (control.a >= 360) {
    control.a = 0;
}
if (control.a < 0) {
    control.a = 360;
}

Edit: I forgot to mention, you need this code to utilize the output.
if (keys[SHIFT]) {
    if(rotation > 0) {
        control.a += control.agility;
    }
    if(rotation < 0) {
        control.a -= control.agility;
    }
}

Now, everything should be working properly.

For clarity, here is the entire fixed program: program .
I hope this helps!



P.S. Because var rotation is larger the further your mouse angle is from the angle of the ship, you could tell the ship to rotate faster or slower as well.

P.P.S. The ship will vibrate around its focal point. This is because var rotation is not being updated when control.a is being updated during the execution stage (the code shown in my edit). The code below fixes the problem, albeit not very efficiently.
if (keys[SHIFT]) {
    if(rotation > 0) {
        control.a += control.agility;
    }
    var rotation = atan2(mouseY-height/2,mouseX-width/2)-control.a;
    if(rotation >= 180) {
        rotation -= 360;
    }
    if(rotation < -180) {
        rotation += 360;
    }
    if(rotation < 0) {
        control.a -= control.agility;
    }
}

This is one tough navy, boy. They don't give you time off, even for being dead. -Clay

Storm Petrel

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #20564 by IronyMan
Wow. I see now exactly what was wrong.

I integrated your perfect fix into my latest version; it now also has rotational momentum.

This is my profile:
www.khanacademy.org/profile/IronyMan/

It's the second most recent program.

I am impressed. That was a really in-depth solution and explanation. Thank you!

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #20565 by IronyMan
Well, I just checked and that program is not visible on my profile when I'm logged out. Bah.

I'll just make a new profile. I don't know what's wrong, I don't care, and I'm too lazy to work out a less drastic solution. I'll let you know when the new profile is up.

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago - 3 years 4 months ago #20566 by Chessking
But if you create a new profile, you won't have your own username for that profile. :(
My research has not revealed anything indicating that this has occurred before. I would suggest you copy the program's code into a new program and save it again, then see if it shows up when you are logged out.

Edit: perhaps your account got disconnected from your Google account or something like that.

Also, I am glad you found my post helpful. I try to make my explanations as clear as possible, but rarely get feedback telling me whether I succeeded.

This is one tough navy, boy. They don't give you time off, even for being dead. -Clay

Storm Petrel

Please Log in or Create an account to join the conversation.