Page 1 of 2

Collision with only certain objects?

Posted: Fri Jan 02, 2015 10:02 pm
by Bya
I had a previous thread here where I wanted to be able to find whether or not certain objects collide, but my current problem is a bit different.
viewtopic.php?f=4&t=78736

Right now, I have three entities, the player, an enemy, and a "hitbox". I want to be able to check whether or not the player's hitbox collides with the enemy, but the hitbox acts like a physical, tangible object, which is something I don't want. Ideally, the hitbox should exist only when I hit the attack button (which it does), and NOT exert forces on the player, which it currently does (It's the red square). I want the hitbox to be more of an intangible object, or at the very least, not apply any forces on the player or enemy. Here's an example of what I currently have.

Image

Base code for my objects is below.

Code: Select all

	--physics engine below
	love.physics.setMeter(64)
	world = love.physics.newWorld(0, 200, true)
	world:setCallbacks(beginContact, endContact, preSolve, postSolve)
	text = ""
	persisting = 0
	
	
	
	player = {}
	player.b = love.physics.newBody(world, 400, 200, "dynamic")
	player.b:setMass(10)
	player.s = love.physics.newRectangleShape(32,54)
	player.f = love.physics.newFixture(player.b, player.s)
	player.f:setRestitution(0.4)
	player.f:setUserData("Knight")
	player.x = 50;
	player.y = 50;
	
	enemy = {}
	enemy.b = love.physics.newBody(world, 300, 200, "dynamic")
	enemy.b:setMass(10)
	enemy.s = love.physics.newCircleShape(20)
	enemy.f = love.physics.newFixture(enemy.b, enemy.s)
	enemy.f:setRestitution(0.4)
	enemy.f:setUserData("Enemy")
	
	static = {}
		static.b = love.physics.newBody(world, 325, 600, "static")
		static.s = love.physics.newRectangleShape(650,1)
		static.f = love.physics.newFixture(static.b, static.s)
		static.f:setUserData("Floor")
	
	swordhitbox = {}
		swordhitbox.b = love.physics.newBody(world, 10000, 10000, "static")
		swordhitbox.s = love.physics.newRectangleShape(25,25)
		swordhitbox.f = love.physics.newFixture(swordhitbox.b, swordhitbox.s)
		swordhitbox.f:setUserData("swordhitbox")
		swordhitbox.f:setSensor(playerhitboxsensor)
		

Re: Collision with only certain objects?

Posted: Fri Jan 02, 2015 10:26 pm
by joedono
I haven't used love.physics very much, but don't you have to call setSensor() on your Shape instead of your Fixture? The Shape is the piece that has mass and controls collisions, right?

Re: Collision with only certain objects?

Posted: Fri Jan 02, 2015 10:40 pm
by Bya
joedono wrote:I haven't used love.physics very much, but don't you have to call setSensor() on your Shape instead of your Fixture? The Shape is the piece that has mass and controls collisions, right?
I changed my line from
swordhitbox.f:setSensor(playerhitboxsensor)
to
swordhitbox.f:setSensor(swordhitbox.s)

And this seems to do what I want it to, as far as not making the hitbox push upon the player. However, I can't seem to detect when the enemy player collides with the hitbox. I'm trying the following code, with swordhitbox.s also swapped out for just swordhitbox.

Code: Select all

 if a == swordhitbox.s then
			if b == enemy.f then
			text = text.."b2"
			tempenemyimage = images.damaged
			enemyhealth = 0
			end
			end

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 12:04 am
by s-ol
Bya wrote:
joedono wrote:I haven't used love.physics very much, but don't you have to call setSensor() on your Shape instead of your Fixture? The Shape is the piece that has mass and controls collisions, right?
I changed my line from
swordhitbox.f:setSensor(playerhitboxsensor)
to
swordhitbox.f:setSensor(swordhitbox.s)

And this seems to do what I want it to, as far as not making the hitbox push upon the player. However, I can't seem to detect when the enemy player collides with the hitbox. I'm trying the following code, with swordhitbox.s also swapped out for just swordhitbox.

Code: Select all

 if a == swordhitbox.s then
			if b == enemy.f then
			text = text.."b2"
			tempenemyimage = images.damaged
			enemyhealth = 0
			end
			end
swordhitbox.f:setSensor(swordhitbox.s)
should be
swordhitbox.f:setSensor(true)

Also I am pretty sure you actually don't want love.physics if you are programming a platformer (and it looks like thats what you are doing):
Image

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 12:28 am
by Bya
Oh, ok. What should I use instead of love.physics?

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 12:54 am
by Jeeper
Bya wrote:Oh, ok. What should I use instead of love.physics?
You should use whatever you feel like using. I have made a platformer game in box2d (love.physics) and there are many great games that have been made with it, a good example being Concerned Joe. That warning is quite silly to be honest.

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 4:15 am
by Azhukar
Set userdata on your hitbox fixture and check for it in your begin contact callback.
Jeeper wrote:That warning is quite silly to be honest.
Yep it is silly. Box2d is vastly superior to the lua-based physic modules found on these forums, though you need to first understand how to use it whereas the other modules are much more straightforward in both their use and implementation.

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 4:26 am
by Jasoco
I recommend kikito's Bump library. It deals with collision detection and resolution. It made it dead simple to make a platformer without the need for the Box2D library. Box2D really is overpowered for a simple platformer. You end up fighting more with making it not do things it shouldn't do instead spending time doing the things you want it to do.

Some various videos and animations from various stages of the current state of various development.


Image Image

And so on.

Various.

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 10:32 am
by s-ol
Jeeper wrote:
Bya wrote:Oh, ok. What should I use instead of love.physics?
You should use whatever you feel like using. I have made a platformer game in box2d (love.physics) and there are many great games that have been made with it, a good example being Concerned Joe. That warning is quite silly to be honest.
If you aren't going for a physics platformer with things to throw and shoot around it is quite silly. If you are doing anything like SMB it is just wasting time getting a an input scheme to work that feels direct and straightforward but still allows for the unrealistic at best usual control we see in platformers. Using something like bump.lua leaves the movement physics to you and allows for more easily customized movement.
If you're only going to move AABBs around then yes, box2D is overpowered. If you know how to, you can still use it, but especially for less experienced users wrangling box2d around can be quite tedious and that's why that warning message exists.

Re: Collision with only certain objects?

Posted: Sat Jan 03, 2015 12:35 pm
by kikito
Jasoco, thanks for the recommendation, those videos look quite good!

I concur with what others say on this thread: box2d is too complex for a lot of platformers. The main reason I found is that box2d tries to do "realistic physics", but most platformers have custom physics which are "kind of realistic", but not too much. You have to "trick" box2d to make that difference work.

I think box2d is a good match for other kinds of games where realistic physics are desirable. The best case I can think of is a top-down spaceship sim, where ships more according to newtonian physics. Even in that case, you have to be careful about things (i.e. how do you rotate ships with thrusters so that they look where you want them to)