Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / dbWait on a single sprite's movement

Author
Message
Proxin
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location:
Posted: 23rd Feb 2011 09:19
Hey guys,
I have an assignment in my C++ class to create a "game" if you'd call it that, where a sprite follows around the mouse cursor.

Please don't misunderstand my intentions, I have already created the code so I'm not looking for someone to do my homework for me However, I was wondering if anyone could help me with advice.

The problem I am having is deciding how I want to get the sprite to follow my cursor. The sprite needs to follow it, but not stay stuck to it. My code currently just sticks the sprite to the cursor.

What my code does right now is, I have a function called Sprite::follow that simply pastes the image. I have a While LoopGDK loop that does boy.follow(mouseX, mouseY)...
What I think it should be doing is, doing a dbWait that only pauses the sprite and not the cursor's movement (I have another sprite that's supposed to stick to the cursor) It's difficult to describe... My code is below to help better describe it:


I hope I have not been confusing, I am having a hard time describing what I need to do here... I'll try to re-explain:
- There is a car sprite (a single image) that should be attached to the cursor's X and Y coordinates
- There is a boy sprite (a set of images that move, instead of a single image) that follows the car sprite, but does not attach to the mouse's X and Y coordinates like the car sprite does.
- Therefore, there should be some delay in the boy sprite's movement. My issue is here.

How can I get some delay in only the boy sprite's movement, as he follows the car sprite? I tried doing the dbWait in my code, as you can see, but ended up commenting it out because dbWait pauses movement of ALL sprites. So it made the movement very choppy, like playing a new game on an old system.

Hope to hear back soon.
Regards,
-Proxin
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 23rd Feb 2011 20:52 Edited at: 23rd Feb 2011 20:54
Store the mouse coordinates and apply them later - with one loop's delay, several loops delay or some time delay. This way, the sprite will be pasted to the coordinates where the mouse was one (or several) screen updates ago.

Here is a sample code, with only one loop delay. It uses static variables but they could also be member variables of the class.



spriteStartPosX/Y mean the coordinates where the sprite is originally, before it starts moving.

With some work you can develop this idea to apply more delay. For example, five loops delay can be achieved by storing five previous mouse coordinates in an array and always discarding the oldest value.
Proxin
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location:
Posted: 25th Feb 2011 00:06
Mireben,
Thanks for the response
Would this allow the sprite to catch up to the mouse's coordinates as well? Or would it always be a few behind the mouse, even if you stopped moving the mouse?
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 25th Feb 2011 21:08 Edited at: 25th Feb 2011 22:52
It would catch up, because if you call the follow function every loop, then the stationary mouse coordinates will also be stored and eventually used. But catching up could be prevented by comparing the latest mouse coordinates and if they are the same as the previously received values, then don't move the sprite.

I've been thinking about this problem in the meantime. A big drawback of the method I suggested is this: If the required delay (or distance in pixels) between the mouse and the sprite is big, then you potentially need to store lots of coordinates (especially with a high screen refresh rate), or implement a filter which stores only coordinates at defined time intervals. The method also depends either on timing or on screen refresh rate, which is an added difficulty.

I have another idea which is a classic "follow" algorythm. In every loop, calculate the direction vector between the sprite and the mouse, and the distance between them. If the distance is bigger than a required minimum, then move the sprite closer, along the direction vector. If the distance is too small, you either do nothing (let it catch up) or move the sprite away from the mouse.

The advantage is that you don't need to store anything, only calculate coordinate offsets. The main issue here is the calculation.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 25th Feb 2011 22:42 Edited at: 25th Feb 2011 22:43
I put together a little test program which shows the coordinate offset method. I think it works quite well. Here is the code:

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 26th Feb 2011 20:09 Edited at: 27th Feb 2011 11:38
Out of curiosity, I tried out my first idea as well. Here is a sample code that stores mouse coordinates and applies them to the sprite with delay. As you will see, the behaviour of the sprite is different.



EDIT: Optimized the code to avoid copying the whole array every loop.

Login to post a reply

Server time is: 2024-10-02 15:28:51
Your offset time is: 2024-10-02 15:28:51