Compound Shapes?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Withered-Flame
Prole
Posts: 3
Joined: Sun Jul 08, 2018 10:56 pm

Compound Shapes?

Does anyone know if i can make compound collision shapes? Like a ball and a rectangle combined to make a mace or something?
I heard you can have multiple shapes inside a single body. When i did this, they are separate objects. The handle moves independently of the spike ball.(also, the rectangle is always at the same center point as the circle, i would like to position it relative to the circle)

I have toyed with joints, and the rect always stays locked in a rotation, even when i setFixedRotation(false).

Edit:

The rectangle now moves with the circle, but i do not know how to position it relative to the circle. It always stays in the center. Any games i could download that showcases this?

I would like to make a cut out of a circle using another circle once i know how to position shapes/fixtures relative to each other in the same body, like cutting out a circle with a smaller circle to make a crescent moon. Any games i could download that showcases this?

I am currently unaware of how to draw shapes or fixtures to the screen. I have toyed with:

Code: Select all

cat = {}
local image = love.graphics.newImage("cat.jpg")
local width = image:getWidth()
local height = image:getHeight()

cat.new = function(x, y, rad, physics)
local self = self or {}
self.x = x
self.y = y

self.p = {}
self.p.world = physics
self.p.body = love.physics.newBody(self.p.world, self.x, self.y, "dynamic")
self.p.prop = love.physics.newFixture(self.p.body, self.p.shape, 1)

self.p.rectp = love.physics.newFixture(self.p.body, self.p.rects, 1)

local cords = {}
cords = self.p.rectp:getBoundingBox()

self.draw = function () -- Draw
love.graphics.draw(image,(self.p.body:getX()),(self.p.body:getY()),
self.p.body:getAngle(),.1,.1,width/2,height/2)
love.graphics.rectangle("fill",(cords[topLeftX]),(cords[bottomRightY]),100,2)


Full Code:

Code: Select all

cat = {}
local image = love.graphics.newImage("cat.jpg")
local width = image:getWidth()
local height = image:getHeight()

cat.new = function(x, y, rad, physics)
local self = self or {}
self.x = x
self.y = y

self.p = {}
self.p.world = physics
self.p.body = love.physics.newBody(self.p.world, self.x, self.y, "dynamic")
self.p.prop = love.physics.newFixture(self.p.body, self.p.shape, 1)

self.p.rectp = love.physics.newFixture(self.p.body, self.p.rects, 1)
self.p.rectp:setSensor(true)
self.p.prop:setFriction(0.2)

self.draw = function () -- Draw
love.graphics.draw(image,(self.p.body:getX()),(self.p.body:getY()),
self.p.body:getAngle(),.1,.1,width/2,height/2)

end
return self
end

I don't know how bodies, shapes, or fixtures relate to each other very well, so if anyone knows any good resources to learn (other than the wiki, I've tried that.)

I would like some extra collision detection on one side of a circle shape only, like a mouth. I would like this extra entity to not do anything else but detect: as in, it does not interact with the world in any way, just collects data and stays relative to wherever Circle i attach it to.
Last edited by Withered-Flame on Wed Jul 11, 2018 2:54 pm, edited 4 times in total.

DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Compound Shapes?

looks correct to me, you're adding the shape to the body the same way i do

you can use the Fixture:setUserData( value ) to attach some thing to the fixture like a table, you then look for this thing and react appropriately in the physics callback

Withered-Flame
Prole
Posts: 3
Joined: Sun Jul 08, 2018 10:56 pm

Re: Compound Shapes?

What about positioning the rectangle relative to the circle?

pgimeno
Party member
Posts: 1150
Joined: Sun Oct 18, 2015 2:58 pm

Re: Compound Shapes?

Both love.physics.newCircleShape and love.physics.newRectangleShape allow specifying a position.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

Withered-Flame
Prole
Posts: 3
Joined: Sun Jul 08, 2018 10:56 pm

Re: Compound Shapes?

Thank you!

I have one last question about excluding shapes from other shapes hit boxes, like a cookie cutter.

Is it possible to do that and draw it as well?

pgimeno
Party member
Posts: 1150
Joined: Sun Oct 18, 2015 2:58 pm

Re: Compound Shapes?

Not to my knowledge.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Compound Shapes?

Withered-Flame wrote:
Wed Jul 11, 2018 4:46 pm
Thank you!

I have one last question about excluding shapes from other shapes hit boxes, like a cookie cutter.

Is it possible to do that and draw it as well?
you're seriously asking for it now, it's likely not gonna be practical for you (vs simply drawing a concave polygon or maybe two if you want a "donut" (new word))... however scouting for this sort of thing i did find this library:

https://github.com/AlexarJING/polygon/

you would use the boolean "not" operation... i am still myself trying to combine this with my polygon reduction code. Note that you can cut out shapes sure... however, cutting a hole in the circle could not possibly give a valid polygon shape... an advanced 3D or vector program no doubt (i dunno if you recall quake 2 editors), it has to shove like a line leading to the hole (this code this guy wrote doesn't seem to do that bit)

dunno if i explained that well, will draw a picture if you didn't get me

edit: the guy also has an interesting box 2d physics helper library

ivan
Party member
Posts: 1304
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Compound Shapes?

I have one last question about excluding shapes from other shapes hit boxes, like a cookie cutter.
No, Box2D works with convex polygons up to 8 vertices max.
Box2D has several other limitations too like minimum edge length and vertex angle.
In short, you want to avoid long, thin triangles which is not simple.
Most geometry libraries will NOT work well with Box2d.
If the output is for rendering only then yes, you could probably get away with it.
Briefly going over AlexarJING's code - it doesn't look very impressive and I also have doubts about its robustness.
The most popular libs are Clipper (for polygon operations) and Poly2Tri (for triangulation) but you need to use those through FFI. And like I said, their output is not designed for Box2D.
The most sensible approach with Box2D is to use simple, overlapping shapes.
Good luck!

Who is online

Users browsing this forum: molul and 4 guests