Skip to main content
gamedev.net
🔒 Locked

Mouse moving.

Started by Ruben Torres Bonet (The Gamedev Guru) Jun 25, 2004 at 2:37 PM 6 replies 700+ views
Original Post
Hello all!I'm currently working on learning some OpenGL and making some test games.In my game,I make a player struct,which containts X and Y position.I know how to move it,using for example player.x+=speed.But I want the mouse to get it's position(I can get them),and move the player there(that is what I don't know how to do).I was looking at vectors,but can't find a good way to done it.Could anyone help me?Any help,or link to any tutorial or example is greetly appreciated!! thanks a lot guys.
xg0blin
xg0blin
What API are you using? GLUT, Win32, MFC, or what? Depending on what you are using, get the mouse coordinates when a button is clicked (the routines are different depending on the API used), the y-coordinate will be upside down most likely, so flip it, and then you have a vector that is (player.Position, Mouse.Position). Do a little linear interpolation based on that line, and there you have it.
im using win32 one...
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
// /* if we don't set the capture we won't get mouse move
// messages when the mouse moves outside the window. */
SetCapture(hWnd);
mx = LOWORD(lParam);
my = HIWORD(lParam);
if (uMsg == WM_LBUTTONDOWN){
player.pos.x=mx;
player.pos.y=my;
}
if (uMsg == WM_RBUTTONDOWN)
player.pos.y=my;
return 0;

this is what I had the last time,the problem is that I have no aceleration there...im very bad in this sort of things,could you give me a code example for doing that?thank you a lot!
xg0blin
xg0blin
Are you refreshing the display after you update the player x and y positions?
Instruo
Instruo
if its teleporting to the right spot, at least you've got a start ;) to get it to move their over time, you're going to have to setup a basic animation system. basically, you impliment a timer and tell it to move part of the way down the path based on the amount of time that has passed (using linear interpolation, as xg0blin said, to figure out where on the line you'll be). I know that isn't too specific, but if you get anything out of this message, note the keywords "timer" and "linear interpolation" ;) good luck!
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
xg0blin
xg0blin
Quote:
Original post by cofruben
Hey instruo,thanks a lot.I am talking about something like Diablo 2,you click in a spot,and player goes there with speed.So that is what I need to do?if yes,I will look at google.thank you!


Yes, that's exactly what you need to do. Linear interpolation moves something from one state to another. You set up a timer (as he said) and every n milliseconds or whatever move a little farther down your line (the line is from the player to the mouse coordinates) until you get to from the start coordinates to the mouse coordinates. Look up linear interpolation on google, and you should be good.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.