My ongoing newbie questions

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.
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: My ongoing newbie questions

Post by eliasaif »

Here is my solution of moving left and right with my avatar:

Code: Select all

    //Moving left
    vx, vy = playerbody:getVelocity()

    if love.keyboard.isDown(love.key_left) then
        //This gives an acceleration to the avatar body
        newX, newY = 0.5*math.cos(3) + vx, 0.5*math.sin(3) + vy
        
        //Here, I set a maximum speed of the avatar body
        if newX < -13 then
            newX = -13
        end

        //When the new velocity is calculated, it's set to the avatar body
        playerbody:setVelocity(newX, newY)

     //Moving right
    elseif love.keyboard.isDown(love.key_right) then

        newX, newY = -0.5*math.cos(-3) + vx, -0.5*math.sin(-3) + vy

        if newX > 13 then
            newX = 13
        end

        playerbody:setVelocity(newX, newY)
    end
You put this code in the update function. Don't forget to rename "playerbody" with your avatar's body name.
essell
Prole
Posts: 15
Joined: Tue Jun 09, 2009 6:26 pm
Location: Newcastle, UK
Contact:

Re: My ongoing newbie questions

Post by essell »

Great - thanks. Will try it tonight :)
User avatar
Jake
Prole
Posts: 27
Joined: Sun Jul 20, 2008 2:01 pm

Re: My ongoing newbie questions

Post by Jake »

Code: Select all

newX, newY = -0.5*math.cos(-3) + vx, -0.5*math.sin(-3) + vy
What's this all about?
User avatar
Person
Prole
Posts: 39
Joined: Thu Jun 18, 2009 2:35 am

Re: My ongoing newbie questions

Post by Person »

I'm sorry if you don't like me posting my own questions, I just thought this was the most appropriate place for them because of what you said on the first page...
essell wrote:I figured instead of starting threads for each of them, I'd post them all here
I'm attempting to set up a basic menu for my tetris clone (well, it's really an inventory tetris clone :megagrin: ). I want to show an image for each "button" of the menu that when clicked will change the game_state variable to a different value, which tells draw() what part of the game to draw. The problem is that some of these images will be PNG files that include transparent, non-uniform edges (read: not square buttons). Is there anyway for LOVE to tell if the mouse is over a non-transparent pixel? Should I abandon my hopes for a homebrew menu and use an existing library (will this make the buttons easier to code)? Or maybe I should hide the mouse and let users interface with the keyboard only!

I read the documentation and it said that images aren't stored in memory and per-pixel calculations were unavailable, so maybe that puts my homebrew version out of the question. I learned basic programming in C++ and the switch to Lua was hard, so I'm hoping the GUI library is out because they're so complex. The keyboard control method seems to be a code intensive way out and seems limiting to the player (or at least that is my experience).

Q: Which of the three methods (Homebrew, GUI library, and Keyboard Control) would be easiest to code and most usable?
"Here's another curse for you, may all your bacon burn."
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: My ongoing newbie questions

Post by bartbes »

Homebrew and non-square buttons is a pain in the ass, I wouldn't recommend it. GUI libs are of good quality, that might be a way to go, and keyboard-only is another good option as well, so, it's a case of what you prefer, and how much time you have. (building GUIs tends to be a lengthy process)
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: My ongoing newbie questions

Post by eliasaif »

Jake: This code will make the object accelerate smoothley.

Code: Select all

newX, newY = -0.5*math.cos(-3) + vx, -0.5*math.sin(-3) + vy
Ofcourse you can do something like this if you don't want any acceleration:

Code: Select all

//Move left
newX, newY = vx -5, vy

//Move right
newX, newY = vx + 5, vy
But when you do that, the velocity in X-axis will always be a constant velocity. So when you press the button, the object will instantly get the chosen velocity.

So the weird code is for a smooth acceleration. I hope i didn't explain it too messy..
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: My ongoing newbie questions

Post by Robin »

eliasaif wrote:

Code: Select all

newX, newY = -0.5*math.cos(-3) + vx, -0.5*math.sin(-3) + vy
So the weird code is for a smooth acceleration. I hope i didn't explain it too messy..
But why didn't you put the calculated sine and cosine values in variables? They're constant, so there is no point in calculating them every time again.
Help us help you: attach a .love.
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: My ongoing newbie questions

Post by eliasaif »

Good point! :)
User avatar
Jake
Prole
Posts: 27
Joined: Sun Jul 20, 2008 2:01 pm

Re: My ongoing newbie questions

Post by Jake »

eliasaif wrote:Jake: This code will make the object accelerate smoothley.

Code: Select all

newX, newY = -0.5*math.cos(-3) + vx, -0.5*math.sin(-3) + vy
Ofcourse you can do something like this if you don't want any acceleration:

Code: Select all

//Move left
newX, newY = vx -5, vy

//Move right
newX, newY = vx + 5, vy
But when you do that, the velocity in X-axis will always be a constant velocity. So when you press the button, the object will instantly get the chosen velocity.

So the weird code is for a smooth acceleration. I hope i didn't explain it too messy..
But cos of -3 is 0.998629535, then you multiply by -0.5 to get -0.4993.., so really you're only decreasing by about 0.5 each time and you may aswell remove the sines and cosines thus getting what you gave in the second code box.
essell
Prole
Posts: 15
Joined: Tue Jun 09, 2009 6:26 pm
Location: Newcastle, UK
Contact:

Re: My ongoing newbie questions

Post by essell »

For about a week I'd not gotten round to working on my game at all, but just last night i started getting back to work on it, and now have most of what I did working with everything as physics objects instead of just moving pictures :)

To get the ball rolling on those questions again...

Q. How do I limit the top speed of a physics object moving in a certain direction (i.e. the speed at which the player moves retreats to the left in a side scrolling shooter)?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 60 guests