Storing Mouse Trail

Previous topic - Next topic

kaotiklabs

Hi,

I´m trying to undestand how to store a mouse trail.
I will explain, I´m think about games 'like flight control' where a path is given to an object in order to follow it.
How this could be implemented easily? Maybe storing the coords each frame??

And if the destination point is moving how could be the trail updated?
any small code tip will be greatly appreciated.  =D

Vote Cthulhu! Because the stars are right!!!!
Ia Ia Cthulhu F' tang!

Hark0

hmmm Maybe storing X Y pos ONLY when change.... minus data, more precission....

http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic

Hatonastick

#2
Here's a really, really rough example:
Code (glbasic) Select
// --------------------------------- //
// Project: Record last (s) number of mouse positions.
// Start: Friday, July 09, 2010
// IDE Version: 7.341

// The size of our circular array, if you want a bigger
// array change it here
GLOBAL s
s = 10

// Mouse location
TYPE coord
// Mouse x
x
// Mouse y
y
// true if this entry has been written to before and
// false if it hasn't
used
ENDTYPE
// Create our 'circular' (sort of) array
GLOBAL mouseloc[] AS coord
DIM mouseloc[s]
// Pointer to our current position in the array
GLOBAL p
// Pointer to the oldest entry in the array -- initially there
// is no oldest entry, just a current one
GLOBAL q
// Pointer used for printing mouse array in a non-destructive
// way eg. by using q to do it for example
GLOBAL r
// Mouse state storage
GLOBAL mx, my, b1, b2

// Initialise

// Get the initial (current) location of the mouse
MOUSESTATE mx, my, b1, b2
// Store it
mouseloc[p].x = mx
mouseloc[p].y = my
mouseloc[p].used = TRUE

// Set starting values for our pointers
p = 0
q = 0

// Allow the mouse pointer to be displayed in the window
SYSTEMPOINTER TRUE

// Read the current mouse location and store in a circular array if the mouse
// pointer has moved more than 5 pixels from the previously stored location

// Push left mouse button to exit first section
WHILE b1 <> 1
// Get the current location of the mouse
MOUSESTATE mx, my, b1, b2
// Has the mouse moved more than 5 pixels from our last stored position?
IF (ABS(mx - mouseloc[p].x) > 5) OR (ABS(my - mouseloc[p].y) > 5)
// Yes, update pointer
INC p, 1
// If pointer p has hit end of array, wrap
IF p = s THEN p = 0
// Store current mouse location
mouseloc[p].x = mx
mouseloc[p].y = my
// Flag our entry as used
mouseloc[p].used = TRUE

// Update the pointer to the oldest entry
IF q = p THEN q = p + 1
// If pointer q has hit end of array, wrap
IF q = s THEN q = 0
ENDIF
SHOWSCREEN
WEND

// Print out all stored mouse pointer locations in the circular array, from
// oldest to newest

// Set our initial pointer to the current value of the pointer to the oldest
// stored value
r = q
// Run through the array and print our stored mouse locations
FOR i = 1 TO s
// Print a stored mouse location if it has been flagged as used
IF mouseloc[r].used = TRUE THEN PRINT i, mouseloc[r].x, mouseloc[r].y

// Move our pointer 1 position from oldest towards newest
INC r, 1
// Wrap if we've hit the end of the array
IF r = s THEN r = 0
NEXT
// Display what we've been writing to the screen
SHOWSCREEN
// Wait till a mouse button is pushed to exit the program
MOUSEWAIT


Edit: Ok I've altered it so it now has two pointers, one to current (newest) entry, one to oldest.

BTW I've got that showscreen in the loop where there are no graphics being drawn because without it the mouse position doesn't seem to get updated and prevents the program from working properly.  There may be a better way to do all of this, this is just meant to point you in a direction or to sort of explain how you might go about it rather than being an actual solution.  =D

Edit: Removed a statement made by me that was blatantly wrong. :)

Edit:  Updated due to a bug I noticed.  This code is now very messy...

Edit: Hopefully squashed all bugs and neatened up the code a bit.  Unless another bug is found, this is the final version.

Edit: Nope, ok now I hope this is the final version. :)  Made the circular array size easily expandable just by changing the value assigned to 's' at the top.
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Kitty Hello


kaotiklabs

uuuauuu mate.  :enc:
Many thanks.

Im going to try it.
Vote Cthulhu! Because the stars are right!!!!
Ia Ia Cthulhu F' tang!