Collision with only certain objects?

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.
Bya
Prole
Posts: 20
Joined: Sat Sep 06, 2014 4:24 am

Collision with only certain objects?

Post 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)
		
User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: Collision with only certain objects?

Post 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?
Bya
Prole
Posts: 20
Joined: Sat Sep 06, 2014 4:24 am

Re: Collision with only certain objects?

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Collision with only certain objects?

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Bya
Prole
Posts: 20
Joined: Sat Sep 06, 2014 4:24 am

Re: Collision with only certain objects?

Post by Bya »

Oh, ok. What should I use instead of love.physics?
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Collision with only certain objects?

Post 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.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Collision with only certain objects?

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Collision with only certain objects?

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Collision with only certain objects?

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Collision with only certain objects?

Post 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)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests