I'm studying python and I've been able to make tank behavior very well, so I'll introduce it. The behavior is like how to transcribe the movement of a tank into code. I used python, but I think it's very easy to apply in other languages.
-Python 3.9.0 -I am using pygame (library).
・ A little programming knowledge ・ High school physics (mechanics)
Rest assured that high school physics is very easy.
This time, I made two steps to make the movement of the tank. It is ** speed ** and ** direction **. Also, in this game, the w key moves up, the a key moves left, the d key moves right, and the s key moves down.
When asked what speed is, most people probably think of it as "the distance traveled per unit of time." That's right. It causes this in the code. The formula to learn in physics is
x=a*t^2+b*t+x_0
It's like that. t is the time, x_0 is the first position, and a and b are constants. How can this be expressed in programming? As a general premise, this program calculates the tank position difference for each while loop. Let's call it velocity. Returning to the story, to express this formula in programming, it is a good solution to think of t as the time when the arrow keys are pressed. By using this formula, you will be able to write movements that start slowly. But when I get here, I want you to stop slowly so that there is inertia, so I will play with this formula a little more.
x=a*t^2+b*t+x_0-c*t_stop^2
Added a new section at the end. t_stop is a variable that counts when the key is not pressed and the speed is not 0. By adding this, it will start to move slowly and smoothly as in reality and stop slowly and smoothly.
pressed_key=pygame.key.get_pressed()
if speed == 0:
if pressed_key[K_a]:
power_left = 1
if pressed_key[K_d]:
power_right = 1
if pressed_key[K_w]:
power_up = 1
if pressed_key[K_s]:
power_down = 1
if(pressed_key[K_a] or pressed_key[K_s] or pressed_key[K_d] or pressed_key[K_w]): #When you press a key
t += 0.1 #Start measuring time
t_stop = 0
else: #To use inertia when the tank is moving and has speed t_Start measuring stop.
if speed > 0:
t_stop += 0.01
else: #Initialize when speed is 0 and no key is pressed
t = 0
speed = 0
speed = 20*t*t+1*t-18*t_stop*t_stop #Calculation of movement speed
if(speed > 7): #Maximum speed setting
speed = 7
Now that we have calculated the speed in the speed section, we will consider in which direction to allocate that speed. By considering this process, I expressed the inertia when bending. But this formula doesn't follow the laws of physics, so I think there is a better way. However, I think that the inertia of the curve can be expressed with a high degree of accuracy, so please refer to it. First,
power_left = 1
power_right = 1
power_up = 1
power_down = 1
And prepare four variables. And
#Achieve a smooth turn by calculating which rampage the speed is heading for
#Start processing
if pressed_key[K_a]:
power_left += 0.1
last_key = 2
elif(power_left > 1):
power_left -= 0.1
if pressed_key[K_d]:
power_right += 0.1
last_key = 3
elif(power_right > 1):
power_right -= 0.1
if pressed_key[K_w]:
power_up += 0.1
last_key = 0
elif(power_up > 1):
power_up -= 0.1
if pressed_key[K_s]:
power_down += 0.1
last_key = 1
elif(power_down > 1):
power_down -= 0.1
#Processing Exit
In this way, while the arrow keys are pressed, the values of the four variables are incremented. And finally,
x -= speed*hit[0]*power_left / \
(power_left+power_right+power_up+power_down) #Calculation of actual position
x += speed*hit[1]*power_right / \
(power_left+power_right+power_up+power_down)
y -= speed*hit[2]*power_up/ \
(power_left+power_right+power_up+power_down)
y += speed*hit[3]*power_down / \
(power_left+power_right+power_up+power_down)
#End of tank movement process
x and y are the coordinates of the tank (player). Also, the array called hit here is the return value of the collision judgment, and if it hits an obstacle, this value will be 0 and you will not be able to move. By doing this, we were able to achieve a smooth curve. I was able to transcribe that inertia into the code that wouldn't bend even if I went straight and tried to bend suddenly.
I made it with great care, so I hope you find it helpful.
Recommended Posts