2
\$\begingroup\$

I'm making a roller skating game and I would like to player to skid or stop moving skating when they try to move in the opposite direction of their current one. But I'm not sure how to go about this.

I tried computing the the dot product of the input of the current frame and the last and if the product was less than or equal 0 that must mean the player did a 180 and the player's velocity was stopped. But it did not work, the product was always greater than 0,

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You need to cache the delta of direction (angle difference so to speak), check if it is greater than 180 or whatever you want, then play the skid animation/sound. You should write exactly where you got stuck so other members may give on-point answers. \$\endgroup\$ – S. Tarık Çetin Sep 27 '17 at 14:20
  • \$\begingroup\$ okay I added the method I tried to solve this problem \$\endgroup\$ – Donald Sep 27 '17 at 14:54
  • \$\begingroup\$ "but it did not work." How did it not work? What did you expect and what did you get instead? What are you stuck with exactly? Is it detecting the sudden 180° turn? \$\endgroup\$ – Vaillancourt Sep 27 '17 at 15:37
  • \$\begingroup\$ I expected it to stop moving when it detect the change. \$\endgroup\$ – Donald Sep 27 '17 at 16:27
  • \$\begingroup\$ Make sure the velocity vector is normalzed before the dot product \$\endgroup\$ – Bálint Sep 28 '17 at 5:58
0
\$\begingroup\$

Thats how I would do it:

Having the velocity of the skater, I would detect if the player is trying to accelerate into the direction of a negative multiple of the velocity, hence in the opposite direction. If thats the case, the velocity of the skater should decrease by e.g. v*0,3 every second, so he should be able to skate into the different direction pretty quickly again. After the deceleration, he should start accelerating into the new direction again.

Maybe you want to play a scratching sound on turning and make his legs stop moving or have some sparks beneath his skaters.

\$\endgroup\$
0
\$\begingroup\$

The answer to this naturally will depend somewhat on your input method. However, there are some general principles.

The process of skidding occurs when one uses the skates to apply a great deal of force in the opposite direction from that of the skater's movement, and the skates break free. The key takeaway here is that this skidding process is a function of inertia.

The method you describe will tell you whether the input suddenly shifted 180 degrees, but that's not really what you want. What you want to do is determine if the input (which governs the forces the skater is applying) is in the same direction as the skater's momentum, or in the opposite direction. This will tell you if the skater is trying to speed up or slow down.

Depending on your input device, you may want to have a small region around this 180 degree point which causes a skid. It's impossible to reliably input exactly 180 degrees opposite something on most analog devices.

\$\endgroup\$
-1
\$\begingroup\$

Well, in Java, for example, you'd have a KeyListener check for your movements and IF... on a turn you faced the opposite direction of your velocity vector then it could trigger a stop response. Yeah... you'd have to listen for the left or right arrow keys and check for that and then trigger the stopMovement() response. Fact is... you know what would be easier? Detect turning and do a gradual vector decrease of movement to zero. This could all be done on KeyListener in Java very easily. But I see what you want, because a slight turn could slam you into a wall, right? But a 180 turn could eventually stop you in place. So you'd have to grab the turn degree and match it against the forward movement vector to react it into a gradual stop. Probably a wide range too. So it wouldn't need to be exactly 180 but somewhere within that range of degrees. Fact is, facing direction should equal resistance. In other words, if you're ice skating in a fast forward movement vector and you suddenly start to turn... then the vector should account for the turning resistance you just gave. So you'd add a different vector to the forward one to add into the turn... now then, if you turned completely the other way around... just check for a range for that, not 180 specifically but maybe 170-190 degrees opposite of forward vector and plug in HEAVY to a resistance variable for that vector movement.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.