mouse coordinates

Posted By: BastovBros

mouse coordinates - 07/07/09 15:46

The question is: how can I make the cursor appear in the center of the screen after loading the level, so that its new central coordinates become the initial position of the mouse? And how can I make limits for the cursor movement on the screen?
Maybe I've explained my problem a bit confusingly, so a would mention the Counter-Strike game to clarify it:
When I launch the game I can move cursor anywhere I want...

when I start a new level my cursor stands on its place if I don't move it with my mouse....

But when the level is loaded the cursor goes to its new position in the center of the window, and when I move the mouse the cursor starts its movement from its new location....

Thank you in advance.
Posted By: Espér

Re: mouse coordinates - 07/07/09 15:57

vec_set(mouse_cursor, vector(0,0,0));
vec_set(mouse_pos, mouse_cursor);

o.O this?
Posted By: BastovBros

Re: mouse coordinates - 07/07/09 16:13

Thanks,
emmm... almost, but how to make the cursor movable then? it's placed in the 0,0,0 position and stands there all the time...
Posted By: Espér

Re: mouse coordinates - 07/07/09 16:20

if you call it in a while loop, this is not working ^^

my idea:
Code:
var setcursor = 1;

...

while(1)
{
  if(setcursor == 1)
  {
    vec_set(mouse_cursor, vector(0,0,0));
    vec_set(mouse_pos, mouse_cursor);
    setcursor = 0;
  }  
  else
  {
    vec_set(mouse_pos, mouse_cursor);
  }
  wait(1);
}


(not tested)

just set setcursor to 1 for centering the mouse.
Posted By: VeT

Re: mouse coordinates - 07/07/09 16:25

he-he, its almost the same as my yesterday's question smile
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=276885#Post276885
Posted By: BastovBros

Re: mouse coordinates - 07/07/09 18:00

>to Vet
lol, you should have written the solution to your question so that I figure out the solution to mine. Maybe I would not create my thread then...
Hu-Gu, Brat!!.... sorry for my Cyrillic accent smile
>to Espér
Thx for a second try smile Hope it's gonna help....
Posted By: BastovBros

Re: mouse coordinates - 07/08/09 21:25

another question related to mouse movement: I would like to trap my mouse pointer in the center of the screen, but in a circle field. I've found a code in one AUM and it works fine:
Quote:
// trap the mouse in an area that has 200x100 pixels and is located in the center of the screen
var area_x = 200;
var area_y = 100;
BMAP* pointer_tga = "pointer.tga";
function mouse_startup()
{
VECTOR temp1_pos, temp2_pos;
mouse_mode = 2;
mouse_map = pointer_tga;
while (1)
{
vec_set(mouse_pos, mouse_cursor);
temp1_pos.x = (screen_size.x - area_x) / 2;
temp2_pos.x = (screen_size.x + area_x) / 2;
mouse_pos.x = clamp(mouse_pos.x, temp1_pos.x, temp2_pos.x);
temp1_pos.y = (screen_size.y - area_y) / 2;
temp2_pos.y = (screen_size.y + area_y) / 2;
mouse_pos.y = clamp(mouse_pos.y, temp1_pos.y, temp2_pos.y);
wait(1);
}
}

though it keeps the mouse in a rectangular field.....
Any idea to keep the mouse in a circle field? I have been trying to write a code myself, but with no successful result frown
Thanks in advance.
Posted By: VeT

Re: mouse coordinates - 07/08/09 21:32

//Hu-Gu, Brat!!.... sorry for my Cyrillic accent
No problems, as you see, i'm from Ukraine too smile

//Any idea to keep the mouse in a circle field?
First idea - is to calculate distance between center point (center of the circle) and maximum cursor distance (point on the radius of the circle)
Posted By: Joozey

Re: mouse coordinates - 07/08/09 22:22

replace:
Code:
var area_x = 200;
var area_y = 100;
[...]
VECTOR temp1_pos, temp2_pos;

[...]

vec_set(mouse_pos, mouse_cursor);
temp1_pos.x = (screen_size.x - area_x) / 2;
temp2_pos.x = (screen_size.x + area_x) / 2;
mouse_pos.x = clamp(mouse_pos.x, temp1_pos.x, temp2_pos.x);
temp1_pos.y = (screen_size.y - area_y) / 2;
temp2_pos.y = (screen_size.y + area_y) / 2;
mouse_pos.y = clamp(mouse_pos.y, temp1_pos.y, temp2_pos.y);



by:
Code:
var radius = 80; //a circle of radius 100
[...]
VECTOR temp1_pos, temp2_pos;

[...]

vec_set(temp1_pos, mouse_cursor); //set temp1_pos to mouse_cursor coordinates

//set temp2_pos to middle of the screen
temp2_pos.x = screen_size.x/2;
temp2_pos.y = screen_size.y/2;
temp2_pos.z = 0; //don't forget this!! .z wont be at 0 initially, will turn in awkward values at vec_dist)

//mouse is outside circle
if( vec_dist( temp1_pos, temp2_pos ) > radius ) {
//compute new position on the circle
vec_diff(temp1_pos, temp1_pos, temp2_pos); //direction from temp2 to temp1
vec_normalize(temp1_pos, radius); //normalize to the radius
vec_add( temp1_pos, temp2_pos ); //add from screen center
vec_set( mouse_pos, temp1_pos );
}
else {
vec_set( mouse_pos, temp1_pos );
}


Posted By: BastovBros

Re: mouse coordinates - 07/09/09 10:08

>to Vet
I've already had this idea, but my programming skills are to bad to write the code properly...
>to Joozey
THANKS A LOT!!!! this code is awesome! much simplier than mine (which in addition didn't work at all). I've learned a new function for me - vec_normilize smile
Posted By: VeT

Re: mouse coordinates - 07/09/09 10:28

yep, very nice solution smile
Posted By: VeT

Re: mouse coordinates - 07/09/09 11:44

btw, for speed up, you can overload function vec_dist() for 2d vectors, or create new
Code:
var dist2d(VECTOR* vector1, VECTOR* vector2)
{
	VECTOR* temp_v;
	var distance;
	temp_v.x = vector1.x - vector2.x;
	temp_v.y = vector1.y - vector2.y;
	return distance = sqrt(temp_v.x*temp_v.x + temp_v.y*temp_v.y); 
}



This one works fine for me.
Posted By: Joozey

Re: mouse coordinates - 07/09/09 12:51

Just as a side note I forgot to add, I was clumsying around getting it to work but it was acting weird all the time, while in my head the calculations were right. Turned out temp2_pos.z wasn't at 0 (because VECTOR temp2_pos; doesn't initialise its members (x, y, z) to 0, but leaves it with some random pointer address), so vec_dist returned a very awkward value.

Setting temp2_pos.z to 0 helped alot wink.
Posted By: VeT

Re: mouse coordinates - 07/09/09 14:57

what about this initialization ? wink
Code:
VECTOR* temp2_pos = {x=0;y=0;z=0;}


Posted By: Joozey

Re: mouse coordinates - 07/09/09 15:30

Fine as well grin as long as all members are set.
Posted By: FcoElizalde

Re: mouse coordinates - 07/05/10 14:36

Thanks for the mouse_pos limiter, but why it doesnt work when mouse_sync=1; ?
Posted By: Lukas

Re: mouse coordinates - 07/05/10 21:56

Because this syncronises your mouse with the windows mouse cursor. To change its position you can use SetCursorPos.

Btw, why are you posting in a thread that is now almost a year old? wink
© 2024 lite-C Forums