2019-06-15 21:46:55 (edited by kianoosh 2019-06-15 21:47:54)

Greetings. I always tried to achieve what i'm going to explane and I never could think of a good formula for it. I want to handle turning in an fps with mouse, just like swamp. That your turning speed increases based on how fast you move the mouse. I know how to turn the player when the mouse moves, but don't know how to make it depend on how fast the mouse is being moved. Let's assume that my player direction is from 0 to 359. Could I have a tip or something to achieve this? If you're going to write code, write it in any programming language you prefer I believe I can understand it. Well everything but those weird programming languages like brain f.
Thanks for reading.

---
Co-founder of Sonorous Arts.
Check out Sonorous Arts on github: https://github.com/sonorous-arts/
my Discord: kianoosh.shakeri2#2988

2019-06-15 21:56:50

you should check the mouse. depending on the coordinates of the mouse in each frame, you can calculate it.

2019-06-15 22:34:16

Yeah, probably calculating the average mouse speed and applying that to turning is a working way to go in theory. Or, you could set some customizable speed, letting user to choose how fast they'd like to turn. But just a question, doesn't the way Amir suggested slow down the game a bit or add some extra work? Eg if we were to calculate the average speed each time the player moves the mouse.

2019-06-16 03:36:51

It depends if you're using a lib / API / toolkit where you are given the mouse movement, or the mouse position. BGT is movements, java.awt.MouseEvent was position, iirc.
If it's position, you need to get the change from frame to frame. In that case, you save the previous position, and it's just dx = newx-oldx.
With that done, you can basically add dx to your angle.
But there are some things to watch out for. How sensitive should the mouse be? Is there a minimum threshold for how fast it should be moving before it registers? Etc.
For the first, I'd just use a coefficient, and let the user set it, like in Swamp. If you're testing with a fairly normal device, figure out what settings get it the most like Swamp, and make that the default. Or, if you're not using Swamp's defaults, make that whatever setting you're using, and work backward to get the default.

Code examples vary and I haven't done much with the mouse outside of BGT. There was some Java stuff, but that was like 10 years ago and I don't remember the exact class / method / property names. BGT is polling-based, and Java is event-based, so they work pretty differently. Event-based saves you from needing to determine when the mouse moves.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2019-06-16 06:42:06 (edited by kianoosh 2019-06-16 07:03:06)

What do you mean by movement and position? BGT also provided x/y/z variables for the mouse.
Edit: The module i'm using has a function that returns a list containing an x and a y variable, telling how much the mouse moved since the last call to the same function. For instance I can move the mouse just a little bit and it returns (1, 0) meaning that I moved the mouse one unit to the right. I figured how to set the direction basedo f this value but what it returns is not a float so I can't round it to set the speed. Is this the only way of limiting the speed? See the following code please
direction+=mouse.get_rel()[0]-speed
so the greater value  for speed, the slower player turns. Also get_rel()[0] returns the x value.
I wanna make it that the turning speed is faster when the speed variable holds a greater value. Is that possible or I only can show the user low speed/normal speed/high speed like swamp and set the value based on their selection within the code?

---
Co-founder of Sonorous Arts.
Check out Sonorous Arts on github: https://github.com/sonorous-arts/
my Discord: kianoosh.shakeri2#2988

2019-06-16 15:55:34

I don't get entirely what you're asking. But if direction is in degrees, if I understand this correctly, your code will break badly. Let's assume that the player did not move the mouse. That'll return (0, 0). then, mouse.get_rel()[0]-speed will be negative, so it will add a negative value onto the direction, which is probably not what you want. If you want to set the mouse sensitivity, something like this will work:

direction += mouse.get_rel()[0]*sensitivity

If you want to turn the player further if the mouse moves faster, eg if I move 20 coordinates in 0.2 seconds the player should be turned further than if I'd move 20 coordinates in 1 second:

# fps is a variable which specifies the frame rate
mouseXMovement = mouse.get_rel()[0]
# calculate the speed, in units per second. v = s / t wherein v the velocity, s the distance and t the time. fps is expressed as 1/t, (60 fps means one frame is 1/60 seconds). so, we get v = s/(1/fps) = s*(fps/1) = s*fps
speed = mouseXMovement*fps
# further calfulations go here

Another approach is to use an exponential function to calculate the new direction:

direction += sensitivity**mouse.get_rel()[0] # direction will increase faster with swifter movements
or another way:
direction += mouse.get_rel()[0]**sensitivity # direction will increase faster with swifter movements

I have no idea which of these methods works best. You'll have to experiment.

Roel
golfing in the kitchen