can anyone help me with moving my model diagonally and smoothly
Well, what problem are you having, specifically? And have you looked at the code given in the posts above for possible solutions?
This requires a simple calculation.
from panda3d.core import LVector3f
# The speed of movement.
speed = 1
# Forward walking vector.
forward = LVector3f(0, 1, 0)
# Leftward walking vector
leftward = LVector3f(-1, 0, 0)
# We get a vector of movement diagonally, forward to left.
diagonally_walk = forward + leftward
print(diagonally_walk*speed)
out: LVector3f(-1, 1, 0)
# However, we got a speed increase because the resulting vector is not equal to 1,
# let's fix this with normalization.
print(diagonally_walk.normalized()*speed)
out: LVector3f(-0.707107, 0.707107, 0)
im trying to develop a simulation ,im caught up while trying to move the model in a diagonal way
def update_trays(self, task):
"""Updates tray positions during movement."""
if self.is_moving:
if len(self.target_positions) > 0:
#print('len of :: ',len(self.active_tray_indices))
for index in range(len(self.active_tray_indices)):
if not self.initial_shift_done[index]:
tray_index = self.active_tray_indices[index]
current_tray = list(self.trays[tray_index])
current_position_x = current_tray[0].getX()
current_position_y = current_tray[0].getY()
if current_position_x > self.target_positions[index]:
#print(f"Current Speed: {self.speed}")
current_tray[0].setX(current_position_x - self.speed)
print("1111")
if data['curve']=="Yes":
current_tray[0].setY(current_position_y + 0.01)
else:
self.initial_shift_done[tray_index] = True
if not current_position_x> self.target_positions[index]:
for entry in self.collision_handler.entries:
into_node_name = entry.getIntoNode().getName()
current_tray_x = current_tray[0].getX()
current_tray_y = current_tray[0].getY()
print(f"current_tray_x: {current_tray_x}")
print(f"current_tray_y: {current_tray_y}")
if entry.getIntoNodePath().getName().startswith("sensor") and into_node_name != "sensor_3"and data['curve']!="Yes":
current_tray[0].setY(current_position_y + 2 *self.speed)
else:
self.anglee= 70
current_tray_x=current_position_x
current_tray_y=current_position_y
print(f"current_tray_x: {current_tray_x}")
print(f"current_tray_y: {current_tray_y}")
target_tray_x = -0.215535126509532
target_tray_y = 3.442893463290488
# target_tray_x = current_tray_x + 3.2* math.cos(self.anglee)
# target_tray_y = current_tray_y + 3.6* math.sin(self.anglee)
increment_x = 0.01 # Adjust as needed
increment_y = 0.1 # Adjust as needed
while abs(current_tray_x - target_tray_x) > 1e-6 or abs(current_tray_y - target_tray_y) > 1e-6:
# Increment current_tray_x towards target_tray_x
distance_x = target_tray_x - current_tray_x
distance_y = target_tray_y - current_tray_y
# Calculate the distance to the target
distance = (distance_x**2 + distance_y**2) ** 0.5
# If we are close enough to the target, stop moving
if distance < 1e-6:
return task.done # Stop the task
# Calculate the direction to the target
direction_x = distance_x / distance
direction_y = distance_y / distance
# Move towards the target by a fraction of the speed
current_tray_x += direction_x * self.speed
current_tray_y += direction_y * self.speed
# Update the tray position
current_tray[0].setPos(current_tray_x, current_tray_y, 0.8)
# Print current positions for debugging
print(f"current_tray_x: {current_tray_x}, current_tray_y: {current_tray_y}")
else:
self.initial_shift_done = [False] * len(self.trays)
else:
self.initial_shift_done = [False] * len(self.trays)
# Check if all trays have completed the initial shift
if all(self.initial_shift_done):
self.is_moving = False
self.initial_shift_done = [False] * len(self.trays)
return task.cont
this is the code i was trying this out but still facing issue in movement diagonally ,while moving diagonally the x movement is done first and then the y movement it appears as if its shaking and moving and it does not move in the desired direction and smoothly ,the diagonal is from left to right and the object must go from down to top
this the motion im trying to achive
Ah, I see! Thank you for the clarification, and for the code snippet!
Looking at what you have there, I think that the problem lies in your “if”-statements.
Specifically, you have an “if”-statement that checks whether “current_position_x > self.target_positions[index]
”.
As long as that’s true, only the code within that section will be executed, and none of the code within the corresponding “else”–and the code in that section only updates the object’s x-position.
That is, only when that “if”-statement is no longer true will your code reach the section in which the y-position is updated.
(I’ll also note that, later on, you have an “if”-statement that checks for “not current_position_x > self.target_positions[index]
”. But this is just a negation of the “if”-statement that I mentioned earlier, and happens in the “else”-section of that “if”-statement. As a result, if this new “if”-statement is reached, then the former “if”-statement must be false, and so this negation of it must be true.
After all, if this new “if”-statement weren’t true, then “current_position_x
” would be greater than “self.target_positions[index]
”… in which case the earlier “if”-statement would have been true, and so the code would not have reached this new “if”-statement.)