So did that fix the issue you where having below (actually above this post)?Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
Camera Shake? / Collisions / Enemies...
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Camera Shake?
Help us help you: attach a .love.
Re: Camera Shake? / Collisions
That's a reply to my post with the 16 boolean statements.@Meeloq on Twitter wrote:@zac352 lol. I usually avoid helping people with boolean operators for exactly that reason.
Hello, I am not dead.
Re: Camera Shake?
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:Robin wrote:So did that fix the issue you where having below (actually above this post)?Ryne wrote:EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
This is love.load
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
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
Code: Select all
if (these 2 boxes collide) then
-- DO THIS
end
Code: Select all
if SOMETHING = true then
-- do this --
end
@rynesaur
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
Hello, I am not dead.
Re: Camera Shake? / Collisions
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 likezac352 wrote:*adds comments, reposts* I'm not sure if this is helping at all.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
Code: Select all
if COLLIDE = TRUE then
-- DO THIS --
end
@rynesaur
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:
The structure of an if-statement is as follows:
Furthermore, this is also possible:
And lastly possible is,
A 'condition' is any statement that evaluates to either true or false.
For example
Evaluates to 'true', that is, any place I put (1==1) where a conditional is required, it will be 'true.'
However,
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.
{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
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
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
Code: Select all
if (condition) then
end
Code: Select all
if (condition) then
{some code}
else
{some other code}
Code: Select all
if (condition) then
{some code}
elseif (some other condition) then
{some other code}
else
{again some other code}
end
For example
Code: Select all
1== 1
However,
Code: Select all
1 ~= 1
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
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
A boolean is just a variable that can either be 'true' or 'false'.
-Ninwa
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
and my function is this:
How exactly do I use this in an if statement. Like If I wanted to do something specific if the function returns true? like
This is frustrating, and probably easier than I'm make it out to be.
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
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
Code: Select all
if MyFunction = true then
-- do this --
end
@rynesaur
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:
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!
Code: Select all
if myFunction() == true then -- myFunction() returned true
-- do stuff
else -- myFunction() returned false
-- do other stuff
end
- 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.
Then your if part would be:
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
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
Code: Select all
if CheckCollision(player, enemy) then
-- oh, noes
end
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
Help us help you: attach a .love.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Camera Shake? / Collisions
There, there.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? likeThis is frustrating, and probably easier than I'm make it out to be.Code: Select all
if MyFunction = true then -- do this -- end
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)
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
Code: Select all
condition1 = 3 == 4 -- condition1 is false
condition2 = 1 == 1 -- condition2 is true
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...
instead.
Code: Select all
if true then
-- this is always executed --
end
if false then
-- this is never executed--
end
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
Code: Select all
Also, you can make functions return true or false.
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
When I write def I mean function.
Who is online
Users browsing this forum: Bing [Bot] and 5 guests