## Camera Shake? / Collisions / Enemies...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Camera Shake?

Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
So did that fix the issue you where having below (actually above this post)?

zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Camera Shake? / Collisions

@Meeloq on Twitter wrote:@zac352 lol. I usually avoid helping people with boolean operators for exactly that reason.
That's a reply to my post with the 16 boolean statements.

Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake?

Robin wrote:
Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
So did that fix the issue you where having below (actually above this post)?
At the risk of sounding like a total noob. The various posts have me confused. I think I'm too stupid to know how to use booleans. This is my code:

Code: Select all

	-- player variables
player = {
hp = 100,
status = "ALIVE",
x = 100,
y = 100,
w = 32,
h = 32,
}

-- enemy variables
enemy = {
hp = 100,
status = "ALIVE",
x = 150,
y = 150,
w = 32,
h = 32,
}

box1x = player.x
box1y = player.y
box1w = player.w
box1h = player.h

box2x = enemy.x
box2y = enemy.y
box2w = enemy.w
box2h = enemy.h


and the function is placed just after love.load and before love.update.

Code: Select all

function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)

if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1    -- Is b2 under b1?
then
return false              -- No collision. Yay!
else
return true           -- Yes collision. Ouch!
end
end

I think I just don't know how to properly use booleans. So I just want to know how to code "if" statements using this function and the boxes? Im not certain how to say:

Code: Select all

if (these 2 boxes collide) then
-- DO THIS
end
You know, normally I would do something like:

Code: Select all

if SOMETHING = true then
-- do this --
end

I just don't know what "SOMETHING" is... Thanks guys. I'm so vastly confused.
@rynesaur

zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Camera Shake? / Collisions

Code: Select all

function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)

if

box2x<box1x+box1w and box2x>box1x and --top left
box2y<box1y+box1h and box2y>box1y or

box2x+box2w<box1x+box1w and box2x+box2w>box1x and --bottom right
box2y+box2h<box1y+box1h and box2y+box2h>box1y or

box2x<box1x+box1w and box2x>box1x and --bottom left
box2y+box2h<box1y+box1h and box2y+box2h>box1y or

box2x<box1x+box1w and box2x>box1x and --top right
box2y+box2h<box1y+box1h and box2y+box2h>box1y

then

return true --The two boxes collide

else

return false --The two boxes do not collide

end
end
*adds comments, reposts* I'm not sure if this is helping at all.

Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake? / Collisions

zac352 wrote:

Code: Select all

function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)

if

box2x<box1x+box1w and box2x>box1x and --top left
box2y<box1y+box1h and box2y>box1y or

box2x+box2w<box1x+box1w and box2x+box2w>box1x and --bottom right
box2y+box2h<box1y+box1h and box2y+box2h>box1y or

box2x<box1x+box1w and box2x>box1x and --bottom left
box2y+box2h<box1y+box1h and box2y+box2h>box1y or

box2x<box1x+box1w and box2x>box1x and --top right
box2y+box2h<box1y+box1h and box2y+box2h>box1y

then

return true --The two boxes collide

else

return false --The two boxes do not collide

end
end
*adds comments, reposts* I'm not sure if this is helping at all.
Thats not the problem I'm having. There doesnt seem to be an issue with the code I'm using I just dont know how to use it, read my previous post. I want to know how to program "if" statements or other variables using the boxes like

Code: Select all

if COLLIDE = TRUE then
-- DO THIS --
end

Obviously that doesnt work, I want to know what WOULD work in that situation..
@rynesaur

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

### Re: Camera Shake? / Collisions

Ryne, the collision detection function you are using is going to return a 'true' value if the two objects are colliding, and a 'false' value if they are not. When a function returns a value you can think of it as "passing it back to the code."

So let's say I had a function which was real simple, all it did was compare two numbers, if the two numbers were the same the function would return true, if they were not they would return false, here's what it would look like:

Code: Select all

function compare(a,b)
if a == b then
return true
end

return false
end

The structure of an if-statement is as follows:

Code: Select all


if (condition) then

end


Furthermore, this is also possible:

Code: Select all


if (condition) then
{some code}
else
{some other code}


And lastly possible is,

Code: Select all


if (condition) then
{some code}
elseif (some other condition) then
{some other code}
else
{again some other code}
end


A 'condition' is any statement that evaluates to either true or false.

For example

Code: Select all

1== 1

Evaluates to 'true', that is, any place I put (1==1) where a conditional is required, it will be 'true.'

However,

Code: Select all

1 ~= 1

That is, the condition "1 is not equal to 1" will return false, because 1 IS equal to one.

With THIS ALL IN MIND, let's return back to our original functions which return values, remember they return true or false, so they can be treated as conditions essentially, so.

Code: Select all

if myFunction(...) then
{some code}
end

{some code} will be executed if myFunction returns a true value.

In the instance of your collision function, it returns true when the two objects are colldiing, so

Code: Select all


if collisionDetection(...) then
{do this if objects are colliding}
else
{do this if they're not, or just use end instead}
end


I hope this helps. I would recommend reading the Lua manual on if-statements and conditionals.

A boolean is just a variable that can either be 'true' or 'false'.

-Ninwa

Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake? / Collisions

Ive read that far in PIL and I still don't fully understand this. So far I have all of the box variables declared in love.load like this

Code: Select all

	box1x = player.x
box1y = player.y
box1w = player.w
box1h = player.h

box2x = enemy.x
box2y = enemy.y
box2w = enemy.w
box2h = enemy.h

and my function is this:

Code: Select all

function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)

if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1    -- Is b2 under b1?
then
return false             -- No collision. Yay!
else
return true         -- Yes collision. Ouch!
end

end

How exactly do I use this in an if statement. Like If I wanted to do something specific if the function returns true? like

Code: Select all

if MyFunction = true then
-- do this --
end

This is frustrating, and probably easier than I'm make it out to be.
@rynesaur

bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

### Re: Camera Shake? / Collisions

You're almost there. When a function returns a value, you can call that function in your code and, at runtime, the call will be replaced with the value it returns. So if I had a function that returned a boolean, I would check it like this:

Code: Select all

if myFunction() == true then -- myFunction() returned true
-- do stuff
else -- myFunction() returned false
-- do other stuff
end
So, since your function is called CheckCollision, just call that in your if statement with the appropriate arguments (box1x, box1y, etc.), and you should be good to go!

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Camera Shake? / Collisions

I just like to note that you don't need all those box variables.

Code: Select all

function CheckCollision(obj1, obj2)
if obj1.x > obj2.x + obj2.w - 1 or -- Is box1 on the right side of box2?
obj1.y > obj2.y + obj2.h - 1 or -- Is box1 under box2?
obj2.x > obj1.x + obj1.w - 1 or -- Is box2 on the right side of box1?
obj2.y > obj1.y + obj1.h - 1    -- Is b2 under b1?
then
return false             -- No collision. Yay!
else
return true         -- Yes collision. Ouch!
end
end
Then your if part would be:

Code: Select all

if CheckCollision(player, enemy) then
-- oh, noes
end
A note on using if-structures: the first part (the one before any else/elseif) will allways be executed unless the expression being evaluated is false or nil. An expression is some calculation that resolves to a certain value, which you can story in a variable, pass to a function, evaluate, etc.
For example, these are all expressions:

Code: Select all

avar
math.random()
3 + 6
foo:bar(3 + 6)
unpack({1,2,3})
"Hello"
4 < x
a == b --note: a = b is NOT an expression, that is a statement.
foo and bar and x or z

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Camera Shake? / Collisions

Ryne wrote:
How exactly do I use this in an if statement. Like If I wanted to do something specific if the function returns true? like

Code: Select all

if MyFunction = true then
-- do this --
end

This is frustrating, and probably easier than I'm make it out to be.
There, there.

Let me try to fill the gaps.

First of all, you don't need to create variables for box1x, box1y, box1w ... etc and assign them to player.x, player.y, player.w, etc. You can use player.x and friends directly when calling the CheckCollision function:
A)

Code: Select all

 -- declaring the function
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
-- body of the function --
end

...

-- invoking the function
CheckCollision(player.x, player.y, player.w, player.h, enemy.x, enemy.y, enemy.w, enemy.h)
When you invoke checkCollision like that, player.x automatically gets "transformed" into box1x, and so happens with the rest of the parameters.

Now, let's talk a bit about expressions. You know that certain expressions return values. For example, the expression 10 + 1 returns the value 11. You can use that value to assign it to a variable:

Code: Select all

x = 10 + 1 -- x is now 11, because 10 + 1 return 11
When you do something == something-else, that is also an expression. It returns something. Contrarily to operator +, which returns numbers, the operator == only returns either true or false, depending on whether something 'is the same as' something else. You can also assign it to a variable:

Code: Select all

condition1 = 3 == 4 -- condition1 is false
condition2 = 1 == 1 -- condition2 is true
Now, for the if part:
B)

Code: Select all

You should not think about it with this in mind:

if something == true then ... .

Think of it in terms of

if expression-that-returns-true then...


The easiest way to grasp it is when you have the values 'true' or 'false' themselves:

Code: Select all

 if true then
-- this is always executed --
end
if false then
-- this is never executed--
end
You don't need to do if something == true then ..., because something == true is an expression that returns true when something is true, and false when something is false. So you can use something directly: if something then ....

Expressions built with the == operator are not the only ones that return true or false. The full list of operators returning true or false is

Code: Select all

 <, >, <=, >=, ==, ~=, and, or 
C)

Code: Select all

Also, you can make functions return true or false.
Remember that I told you that CheckCollision returns true or false? If we put together A), B), and C) this is what you get:

Code: Select all

if CheckCollision(player.x, player.y, player.w, player.h, enemy.x, enemy.y, enemy.w, enemy.h) then
-- do this --
end

I hope this helps.
When I write def I mean function.

### Who is online

Users browsing this forum: No registered users and 45 guests