This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheGrrMan
TheGrrMan wrote ...

2023/1/19

How to stop actors from "sliding" after calling the move() function and changing orientation

TheGrrMan TheGrrMan

2023/1/19

#
I have a knight subclass. It constantly finds the nearest enemy and orients to him, then moves accordingly. But when his orientation changes while moving, he continues moving in the direction he was originally, giving him the appearance of 'sliding' and not looking like he's moving naturally. A diagram of what happens (if that helps) this is my current code:
if(FindClosest()!= null) {
                   //keep moving towards nearest enemy
                   this.turnTowards(FindClosest().getX(),FindClosest().getY()); //turn towards nearest enemy
                   this.move(speed); //move
            }
Any help would be greatly appreciated!
danpost danpost

2023/1/20

#
TheGrrMan wrote...
I have a knight subclass. It constantly finds the nearest enemy and orients to him, then moves accordingly. But when his orientation changes while moving, he continues moving in the direction he was originally, giving him the appearance of 'sliding' and not looking like he's moving naturally. A diagram of what happens (if that helps) << Code Omitted >>
Show code of move(int) method. If speed has a low value, then the moving direction may not be exactly in the direction of rotation. Due to this, you may require a smooth moving algorithm (either greenfoot's SmoothMover class or my QActor class). My QActor class provides for both more precise movement and more precise rotations. See my Support Classes by danpost collection.
TheGrrMan TheGrrMan

2023/1/23

#
The move() is called with an int of 1 or 2. I've never seen a SmoothMover class, can you give me instructions on how to initialize and use it? I don't want the change to be super big, mostly because I have a lot of code written already and I don't want to have to make a huge shift in how my code works.
danpost danpost

2023/1/23

#
TheGrrMan wrote...
can you give me instructions on how to initialize and use it?
With you main menu bar, select "Edit >> Import class...". Then, select the SmoothMover class and click the "Import" button. DO NOT modify the class in any way. Lastly, have the Actor subclasses that you want precision movement added to extend the SmoothMover class:
// change
public class Knight extends Actor

// to
public class Knight extends SmoothMover
Now, familiarize yourself with the method provided within the SmoothMover class and use them normally from the Knight class (or any other subclass of SmoothMover).
TheGrrMan TheGrrMan

2023/1/24

#
Wow! It's that simple? Thanks so much man! EDIT: The SmoothMover class helped (smoother movement, not sliding as much) but they still slide a little bit. I don't see an import class option or your QActor class, how do I access this?
danpost danpost

2023/1/24

#
TheGrrMan wrote...
your QActor class, how do I access this?
It can be found in my Asteroids w/Improved QActor class scenario. Again, it is not to be modified in any way -- just used as is. Download the scenario and open in greenfoot. Then, you can copy/paste the class into your scenario. It does work differently than the SmoothMover class, however. Open the class and read through the instructions carefully. Actor subclasses requiring it should extend the QActor class.
You need to login to post a reply.