PDA

View Full Version : moving towards mouse


xdeath
09-29-2008, 03:34 AM
don't worry i have managed to code this my self so don't worry replying

drumn4life0789
09-29-2008, 04:25 AM
your going to have to make a variable and call it speed

do like speed equals 10. then say the y value of your character speed until it gets to the mouse y

same thing for x

xdeath
09-29-2008, 07:39 AM
ok this is meant to be for a top down shooter so how exactly would i code what you just said?

beacuse i couldn't think of a way of making it do that. my head is a big mess right now after trying to do the whole move towards mouse.

Draxus
10-01-2008, 06:19 PM
I don't know if you're using as2 or 3 but here is the basic idea. "thing" is the object you're moving. Adjust "power" to change the speed

power = 11
speedX = 0
speedY = 0

This stuff goes on an EnterFrame:
dx = thing.x - xmouse
dy = thing.y - ymouse
direction = Math.atan2(dy,dx)
speedX = Math.cos(direction) * power
speedY = Math.sin(direction) * power
thing.x += speedX
thing.y += speedY

xdeath
10-03-2008, 03:55 AM
so if i wanted to make my character move towrds a mouse in AS2 i would need to write this?

onClipEvent(load){ // when game starts up,
power = 11 // variable which controls speed of this object when it moves.
speedX = 0 // ???
speedY = 0 // ???
}
onClipEvent(enterFrame){ // this is like a way of saying always do what is below.
dx = this._x - _xmouse // detirmines distance maybe i really don't know what this does?
dy = this._y - _ymouse // ???
direction = Math.atan2(dy,dx) // ???
speedX = Math.cos(direction) * power // ???
speedY = Math.sin(direction) * power // ???
if (Key.isDown(Key.UP)){ // when up arrow key is being pressed down,
this._y += speedY // move character towards mouse?
}
if (Key.isDown(Key.DOWN)){ // when down arrow key is being pressed down,
this._y -= speedY/2 // move character away from mouse at half the speed?
}
if (Key.isDown(Key.RIGHT)){ // when right arrow key is being pressed down,
this._x += speedX // move character side-ways (right) away from mouse.
}
if (Key.isDown(Key.LEFT)){ // when left arrow key is being pressed down,
this._x -= speedX // move character side-ways (left) away from mouse.
}
}


well wheather that works or not i will have to find out later, but could you comment your code and what each variable is for so i understand your code as i don't really understand math functions.

Draxus
10-03-2008, 11:18 AM
I'm not sure what you're trying to do. Do you just want to have your character move left when you press left, right when you press right, etc? Saying you want him to "move right, away from the mouse" is confusing. Your character either moves right, or not... doesn't matter where the mouse is, right will always be right. The code below will make your character move towards the mouse when you press UP and away from the mouse when you press DOWN. I'm not sure what you'd want to do with the left and right keys with a control scheme like this, so I took them out.


onClipEvent(load)
{
power = 11 //The speed your character will move in the direction we calculate below
speedX = 0 //Just setting up the variable
speedY = 0 //Just setting up the variable
}
onClipEvent(enterFrame)
{
dx = this._x - _xmouse //Find distance on the x-axis between your character and the mouse.
dy = this._y - _ymouse //Find distance on the y-axis between your character and the mouse.
direction = Math.atan2(dy,dx) //Find the direction to move
speedX = Math.cos(direction) * power //Find number of pixels to move on the x axis, multiply it by your character's power.
speedY = Math.sin(direction) * power //Find number of pixels to move on the y axis, multiply it by your character's power.
if (Key.isDown(Key.UP))
{
//Move character based on his speed, calculated above
this._x += this.xSpeed
this._y += this.ySpeed
}
if (Key.isDown(Key.DOWN))
{
//Move character based on his speed, calculated above
this._x -= this.xSpeed
this._y -= this.ySpeed
}
}


You'll need to read up on some trig if you want to understand the math behind the atan2, sin and cos functions. If you want to move your character straight right or left, just add or subtract some arbitrary number from the characters _x. Your control scheme is odd though... I'm not really sure what you're going for.

xdeath
10-04-2008, 03:53 AM
this doesn't work.
all i want to do is make my character move towards the mouse when the arrow keys are being pressed. is that really so hard?

Draxus
10-04-2008, 09:43 PM
What isn't working? Did you get an error message, or is it just not behaving as you'd like?

Let's say your character is at x:100 y:300, and your mouse is is at x:400 y:50. There is only one direction that is toward the mouse, and one that is away. There is no "move right, away from the mouse"... you either move away from it or not. The code above will find the direction from your character to your mouse, and move him straight towards the mouse, or straight away.

Moving an mc towards the mouse is extremely simple, any confusion is the result of your being unable to accurately describe what you'd like it to do, or misusing the code in some way.

xdeath
10-05-2008, 06:19 AM
it didn't work at all. i decided later on that day that if no one could show me how to do the top down movement i was after then i had no choice but to go back to old way were you can only move up,down,left or right and you rotate towards the mouse.

but if anyone could help me do this that would be good. as i could make it into like an option or something.

chriswa
10-08-2008, 08:02 AM
By "move right", xdeath means perpendicularly to the direction facing the mouse.

xdeath:

The code provided to you calculates the x and y components of a vector pointing toward the mouse. To move toward the mouse, you must use both x and y:

if (Key.isDown(Key.UP)){
// move forward
thing.x += speedX
thing.y += speedY
}

To move in another direction, you need to adjust the direction used in the calculation. I've tried to make this as simple as possible, even at the expense of readability. ;)

// decide on speed and direction
if (Key.isDown(Key.UP)){ // when up arrow key is being pressed down,
relativeDirection = 0 // 0 is forward
speed = 10
}
if (Key.isDown(Key.DOWN)){ // when down arrow key is being pressed down,
relativeDirection = Math.PI // about face (180 degrees)
speed = 5 // half speed
}
if (Key.isDown(Key.RIGHT)){ // when right arrow key is being pressed down,
relativeDirection = Math.PI / 2 // 90 degrees
speed = 10
}
if (Key.isDown(Key.LEFT)){ // when left arrow key is being pressed down,
relativeDirection = -Math.PI / 2 // -90 degrees
speed = 10
}

// determine angle to mouse
angleToMouse = Math.atan2( this._y - _ymouse, this._x - _xmouse )

// determine movement direction
direction = angleToMouse + relativeDirection

// determine velocity vector
velocityX = Math.cos(direction) * speed
velocityY = Math.sin(direction) * speed

// move!
this._x += velocityX
this._y += velocityY

xdeath
10-09-2008, 12:55 AM
By "move right", xdeath means perpendicularly to the direction facing the mouse.

xdeath:

The code provided to you calculates the x and y components of a vector pointing toward the mouse. To move toward the mouse, you must use both x and y:

if (Key.isDown(Key.UP)){
// move forward
thing.x += speedX
thing.y += speedY
}

To move in another direction, you need to adjust the direction used in the calculation. I've tried to make this as simple as possible, even at the expense of readability. ;)

// decide on speed and direction
if (Key.isDown(Key.UP)){ // when up arrow key is being pressed down,
relativeDirection = 0 // 0 is forward
speed = 10
}
if (Key.isDown(Key.DOWN)){ // when down arrow key is being pressed down,
relativeDirection = Math.PI // about face (180 degrees)
speed = 5 // half speed
}
if (Key.isDown(Key.RIGHT)){ // when right arrow key is being pressed down,
relativeDirection = Math.PI / 2 // 90 degrees
speed = 10
}
if (Key.isDown(Key.LEFT)){ // when left arrow key is being pressed down,
relativeDirection = -Math.PI / 2 // -90 degrees
speed = 10
}

// determine angle to mouse
angleToMouse = Math.atan2( this._y - _ymouse, this._x - _xmouse )

// determine movement direction
direction = angleToMouse + relativeDirection

// determine velocity vector
velocityX = Math.cos(direction) * speed
velocityY = Math.sin(direction) * speed

// move!
this._x += velocityX
this._y += velocityY

thanks burt this is already solved. i coded it myslef. and your code wouldn't work the way i wanted anyway, but thanks for trying.