Want to make the mouse the "forward" direction of the player

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
dementia_patient567
Prole
Posts: 12
Joined: Sun Jan 27, 2013 6:00 am

Want to make the mouse the "forward" direction of the player

Post by dementia_patient567 »

So here's my game that I've been working on. I've taken a break from it for a week or two because I got frustrated with it but now I'm ready to learn more about Lua and Love(No idea how to make the accent there.)

The reason I got frustrated is I want the player to move towards the mouse when they press "w." Right now, the game uses wasd to move around but "w" is "up" in terms of how the screen is. I need "up" to be where-ever the mouse happens to be. I'd like to keep the momentum thingy I've got going. I know about the atan2 thing, hence why my ship spins with the mouse. But the whole "moving towards the mouse" is throwing me off.

I'd still like the "asd" keys to move the ship "left, right, back" but only in relevance to where the mouse is, since the pointer is supposed to be "forward."

It's probably something right in front of me, like most of my problems, but as a beginner, I can't seem to figure it out.
Colored Polygon Battles.love
(50.01 KiB) Downloaded 170 times
WASD - move the player
Right Mouse - fire
Scroll Wheel - Change active colors
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Want to make the mouse the "forward" direction of the pl

Post by substitute541 »

I didn't understand your question so, according to the behavior of the game, you want it to move TOWARDS the mouse when you press W. Also, if that were to be fixed, and you want your left/right arrow keys to move relative to the angle of the ship, that.. would kinda be weird (the ship would be moving sideways). So, I won't do that part unless you reply saying that you REALLY want that.

The solution to your problem needs both cosine and sine functions. Because functionally, moving forward and backward is the same (only difference is that moving backward has a negative force), I decided to add that all in a function.

Code: Select all

function addForce(object, angle, magnitude, dt)
    object.x = object.x + (math.cos(angle) * magnitude) * dt
    object.y = object.y + (math.sin(angle) * magnitude) * dt
end
In here, object is an object with a .x and .y property (the coordinates). All you have to specify is the angle of the force vector (if you know what that means) and the magnitude (aka the "length" of the vector, in this case, the speed). I could go on and explain why you use cosine and sine and blah blah blah, but I just made 2 blog posts just for that.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Want to make the mouse the "forward" direction of the pl

Post by micha »

Sideways movement isn't difficult to implement either. To rotate a vector by 90 degrees, simply swap the two entries and multiply one with -1. In other words, to rotate vector (a,b) you get (-b,a) (or (b,-a) depending on rotation direction).

So you get

Code: Select all

function addForceSideways(object, angle, magnitude, dt)
    object.x = object.x - (math.sin(angle) * magnitude) * dt
    object.y = object.y + (math.cos(angle) * magnitude) * dt
end
(same code as above, only swaped sin and cos and put one minus instead of +.

I want to add an off-topic suggestion:
The particles on the back of the ship look great, but are only physically plausible, when the ship accelerates. So once you have implemented the "towards-mouse-movement", I suggest you only spawn particles, when the player presses w. You can indicate the controls, the player hits. If the player presses a, for instance, you would spawn particles on the rights side of the ship.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests