Getting the Length and Width of a Random Rectangle

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_
Prole
Posts: 5
Joined: Sat Jan 17, 2015 3:19 am

Getting the Length and Width of a Random Rectangle

Post by _dementia_ »

Hello! Been a while but I'm finally coding my 2nd 'game' in love and I've gotten a bit stuck.

I have some randomly sized rectangles that I need to get the width and length of to determine if there's a collision to do.

Here is my enemy_load() function

Code: Select all

  enemy = {}
  enemy.x1=0
  enemy.x2=love.graphics.getWidth()
  enemy.y1=0
  enemy.y2=love.graphics.getHeight()
  enemy.minSize1=15
  enemy.minSize2=enemy.minSize1*2
  enemy.maxSize1=30
  enemy.maxSize2=enemy.maxSize1*2
  enemy.initspeed=20
  enemy.timer_init=1
  enemy.timer=enemy.timer_init
  
  function enemy_spawn(x,y,width,height,speed,r,g,b)
    table.insert(enemy, {x=x,y=y,width=width,height=height,speed=speed,r=r,g=g,b=b})
  end
As you can see, there is no enemy.length or enemy.width. I later use an ipairs loop to get the v.width and v.height of the rectangle.

When A timer comes up, this runs:

Code: Select all

    enemy_spawn(math.random(e.x1,e.x2),math.random(e.y1,e.y2),math.random(e.minSize1,e.minSize2),math.random(     e.maxSize1,e.maxSize2),enemy.initspeed,math.random(0,255),math.random(0,255),math.random(0,255))
It makes sense in context, although I'm sure I'm going about it the hard way. Every variable of the rectangle is randomized including x,y,length and width. Length and width should have been inserted with the enemy_spawn() function, right? Well it's not, apparently. Because when I run this:

Code: Select all

for i,v in ipairs(enemy) do
 for bi,bv in ipairs(bullet) do
  if bv.x > v.x+v.width or bv.x < bv.width-bv.w or bv.y < v.y+v.height or bv.y > v.height-v.height then
 end
 end
it gives me this when a bullet comes out:
loveerror.png
loveerror.png (18.39 KiB) Viewed 6094 times
Which means that it's not creating a width or height variable and I'm quite lost on it. I don't see a way to do like love.graphics.getWidth() for rectangles and I don't know how to do it myself if it won't insert the way I'm doing it.

I'm probably missing something obvious!
EOGiI.love
(4.58 KiB) Downloaded 1020 times
I should warn that my code is a warzone of uncommented, unorganized scrap. I'm simply doing a crummy game to learn things for better things. Hopefully that doesn't stop one of you smart people from helping me out!

Cheers!

Edit: I commeted out my bugged collision code. Here is a version of the game that's "playable." You should see what's up.
EOGiI2.love
(4.69 KiB) Downloaded 1020 times
Right now 1,2 and 3 change the RGB colors of the character and the bullets it fires. The goal is that you have to get your RGB colors close to the enemy's RGB colors(which are completely random at spawn) and it will kill them/make them lose health.
Also the border has a threshold like that already, if your RGBs are more than 40 units away, it will make the walls grow, if you're within 40, it'll make them grow. Still have to polish how it works, but right now it's just testing an idea I have.
Doesn't really play well but it's the best I've done so far.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Getting the Length and Width of a Random Rectangle

Post by Azhukar »

The problem is with bv.width not v.width.
_dementia_
Prole
Posts: 5
Joined: Sat Jan 17, 2015 3:19 am

Re: Getting the Length and Width of a Random Rectangle

Post by _dementia_ »

Here's a little bit more about the error with a collision system I used in another game of mine that should work well with the rectangles of this game...But this picture demonstrates why I think it's the enemy's v.width and not the bullet's bv.width
error 3.png
error 3.png (82.84 KiB) Viewed 6053 times
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Getting the Length and Width of a Random Rectangle

Post by Azhukar »

See for yourself and separate your if condition so v.width and bv.width are on different lines, it will pop an error on line with bv.width.

The other unrelated error you just posted is because you have a typo there, see obj2 vs ob2.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Getting the Length and Width of a Random Rectangle

Post by zorg »

Azhukar wrote:The other unrelated error you just posted is because you have a typo there, see obj2 vs ob2.
And if i'm understanding that code bit right, you also have obj1 vs ob1 in the line under; that would probably error as well...as well as the ones on the two lines after; you really should name your variables a bit less confusingly :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
_dementia_
Prole
Posts: 5
Joined: Sat Jan 17, 2015 3:19 am

Re: Getting the Length and Width of a Random Rectangle

Post by _dementia_ »

you really should name your variables a bit less confusingly :3
I'm really realizing this with this game. I have plans to help me out a lot in the next game, but this one isn't worth a full rewrite full of features I don't yet know exactly how to program. This is a huge upgrade from my last game in terms of complexity and whatnot. I'm just wanting to get it working, get some feedback and make Every Other Game is Inferior 3 my best shitty work yet :D
_dementia_
Prole
Posts: 5
Joined: Sat Jan 17, 2015 3:19 am

Re: Getting the Length and Width of a Random Rectangle

Post by _dementia_ »

Thank you for your help! I've managed to get it all stable. Still a long way to go. I'm postng an exe because that's what I have ready. If you want a .love, just yell at me!
Game.zip
(3.31 MiB) Downloaded 299 times
Post Reply

Who is online

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