Author Topic: Hitboxes  (Read 6964 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Hitboxes
« on: September 24, 2012, 01:50:13 pm »
While working on a project I found myself in need of detecting whether the mouse was clicked inside a certain area or not. I thought it would be pretty simple since I only needed square 'hitboxes'. I wrote some simple code and figured to share it in case it might be useful to others.

Hitbox code:
Code for the hitbox:
Code: [Select]
HitBoxSquare = class()
function HitBoxSquare:init(xIn, yIn, width, height)
     self.x = xIn
     self.y = yIn
     self.width = width
     self.height = height
end

function HitBoxSquare:contains(xMouse, yMouse)
    local sw = self.width
    local sh = self.height
    return (xMouse>=(self.x)) and xMouse<=(self.x+sw) and yMouse>=(self.y) and yMouse<=(self.y+sh)
end
Mandatory code:
To track mousepresses:
Code: [Select]
function on.mouseDown(x, y)
    mouseX = x
    mouseY = y
    --Other code here
end
To track mousemovement:
Code: [Select]
function on.mouseMove(x, y)
    mouseX = x
    mouseY = y
    --Other code here
end

How to use:
Create the hitbox:
Code: [Select]
hitBox = HitBoxSquare(x, y, width, height)Check if the mouse is clicked inside the hitbox:
Code: [Select]
if hitBox:contains(mouseX, mouseY) then
    --Do something here
end

Using multiple hitboxes:
If you want to use more then one hitbox, checking them by hand is a little too much work. Here's where tables come in handy!
Code: [Select]
-----------------------------------------------
-- Checking multiple hitboxes, the fast way. --
-- ElementCoder, 2012                        --
-----------------------------------------------
HitBoxSquare = class()
function HitBoxSquare:init(xIn, yIn, width, height)
     self.x = xIn
     self.y = yIn
     self.width = width
     self.height = height
end

function HitBoxSquare:contains(xMouse, yMouse)
    local sw = self.width
    local sh = self.height
    return (xMouse>=(self.x)) and xMouse<=(self.x+sw) and yMouse>=(self.y) and yMouse<=(self.y+sh)
end

hb1 = HitBoxSquare(10, 10, 10, 10)
hb2 = HitBoxSquare(10, 25, 10, 10)
hb3 = HitBoxSquare(10, 40, 10, 10)
hitbox = {hb1, hb2, hb3}
hitboxname = {"Hitbox 1", "Hitbox 2", "Hitbox 3"}

function on.paint(gc)
    gc:drawRect(10, 10, 10, 10)
    gc:drawRect(10, 25, 10, 10)
    gc:drawRect(10, 40, 10, 10)
end

function on.mouseMove(x, y)
    mouseX, mouseY = x, y
    for i = 1, 3 do
        if hitbox[i]:contains(mouseX, mouseY) then
            print(hitboxname[i].." contains the mouse!")
        end
    end
end
« Last Edit: October 14, 2012, 01:42:30 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Hitboxes
« Reply #1 on: September 24, 2012, 05:01:39 pm »
Nice! I used a system similar to this with my Checkers game for mouse controls.

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Hitboxes
« Reply #2 on: October 11, 2012, 08:57:31 pm »
Oh wow this is great! I could see this coming into use for some ideas that I have that involve a mouse pointer.
So theoretically you could have several hitboxes that you could move around quickly and make the mouse click, all doing different actions right?
« Last Edit: October 11, 2012, 08:58:38 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Hitboxes
« Reply #3 on: October 13, 2012, 02:03:20 pm »
Yep, you would define every hitbox by doing so:
Code: [Select]
hitBox1 = HitBoxSquare(x, y, widht, height)
hitBox2 = HitBoxSquare(x, y, widht, height)
hitBox3 = HitBoxSquare(x, y, widht, height)
And then you would check for mouse presence by doing this:
Code: [Select]
hitBox1:contains(mouseX, mouseY)
hitBox2:contains(mouseX, mouseY)
hitBox3:contains(mouseX, mouseY)
But this would not work very well if X hitboxes would be to overlap each other (unless you'd want all X actions to be performed ofc).

P.S. Nice to see your still working on projects :) I'm glad I have some time again for programming and such. I can finally start working on projects that have been on hold for a long time :D

Some people need a high five in the face... with a chair.
~EC

Offline Rhombicuboctahedron

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 437
  • Rating: +41/-6
    • View Profile
Re: Hitboxes
« Reply #4 on: October 13, 2012, 02:46:02 pm »
I have only ever used lists with single values, and not classes, but would you be able to use lists to define the hitboxes
Code: [Select]
hitBox={}
hitBox[1] = HitBoxSquare(x, y, widht, height)
hitBox[2] = HitBoxSquare(x, y, widht, height)
hitBox[3] = HitBoxSquare(x, y, widht, height)
That way you could efficiently do something to all of them
Code: [Select]
for num=1,10 do
something
end

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Hitboxes
« Reply #5 on: October 13, 2012, 03:25:42 pm »
I think it is possible. I'll try it tomorrow and if it works I'll post a snippet in the first post to show how to :)
Of course you and others can help too!
[edit]read:  jim we need you D:
« Last Edit: October 13, 2012, 03:30:57 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Hitboxes
« Reply #6 on: October 13, 2012, 04:02:42 pm »
Yes, storing the objects in a table and iterating over them is the way to do it.
I'd love to post some more code, but I'm ill at the moment and can't think straight.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Hitboxes
« Reply #7 on: October 14, 2012, 11:46:06 am »
I made a little example and added it to the main post :)

Some people need a high five in the face... with a chair.
~EC

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Hitboxes
« Reply #8 on: October 14, 2012, 04:32:47 pm »
ElementCoder, I'd suggest you to look through https://github.com/adriweb/EEPro-for-Nspire/blob/master/Global%20Libraries/screen.lua.
It's part of ETK 2, the GUI toolkit I made for EEPro. I don't want to brag about it, but I think there are some interesting things in it, stuff that might give you some ideas. The widgets are defined in widgets.lua, in the same directory as this script.