firing a missile from my ship, is it possible to have an EASY function like Unity 3D

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.
Post Reply
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by DarkShroom »

Say what you will about the messiness of unity, and actually i'm sorta glad to be free of that mess (for the time being lol)... it has this function:

Physics.IgnoreCollision(Collider collider1, Collider collider2, bool ignore = true)

it is the standard way of firing projectiles!

I have already tackled the physics layers system in a previous project with Love 2D, and am likely about to integrate this in my new more swanky program, however it was an awful mindfuck

is there anything here that is as simple and neat as that function?

When you fire a bullet you obviously do not want it to collide with yourself, well this enabled you to pretty much ignore the physics layers (everything pretty much on the same layer), but be able to fire bullets with real physics... does anyone even have a strategy perhaps, as it stands to reason for me that it might even be nice to fire a projectile that CAN collide with myself, this might add extra danger gameplay, like don't get exploded by your own ammo.

Open question, sorry if it's not clear, I'll be sure to elaborate. Physics layers have just never ever really sat comfy with my object orientated way of thinking and also my desire to design games with ambiguity of who is the "GOOD GUYS" and the "BAD GUYS"... i like to break this model myself (also the physics layers, i mean how's that gonna work in multiplayer?).

I mean I get it, I can't do everything in Love 2D but it'd be nice :)

EDIT: omg it's looking like we're intimately attached to box 2d aren't we
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by zorg »

Iirc, love.physics / box2d does support collision groups or whatever they are called; maybe you could define those and build it into your own OOP system or something.

As for what being the "standard" (citation needed, by whom, etc...) way of firing projectiles, a simple

Code: Select all

local bullet = {}
function spawnBullet(x,y,angle,speed)
    table.insert(bullet, {x=x, y=y, m=speed, phi=angle})
end
function love.update(dt)
    -- iterate over bullet table and move stuff
end
function love.draw()
    -- iterate over bullet table, and draw stuff
end
would already do what you said, in the simplest of cases. :3

Edit: See grump's message.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by DarkShroom »

some of that i'd read and some of it made my head hurt, i'd got the last demo working by luck clearly

thanks for the link direct to box 2d (grump), that's really handy

clearly yes (zorg), i will be abstracting this somehow so i can forget about it again


i guess what i might be thinking now is possibly even to make the bullets spawn offset from the player, sounds weird, but i really like the idea of shots coming back in my face after they bounced off walls.


as a further question, if i cracked open Love 2D's hood, something I was hoping to do soon at least just for a peak, and say i found some way of adding the mentioned Unity 3D style feature.... would my code be able to go back into the main fork?

would anyone have the low level knowledge to know if this is possible, i feel maybe it's NOT because i'd have to modify box 2D itself... clearly if I done that it'd essentially be a hack?

Is it possible to ask that question? Maybe I have to head off to bitbucket?

thanks, Rich
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by pgimeno »

Box2D is unlikely to merge such thing, because it already has at least three mechanisms in place to decide whether two shapes will collide.

LÖVE is unlikely to merge such thing, because maintaining the locally modified Box2D in sync with upstream is already painful enough, and having to merge more custom patches probably involves more work than the developers are willing to spend when they want to update the physics engine. Plus, it supports the three mechanisms I mention above.

But you can try. I'm not a LÖVE developer.

If you want to make it as simple as in your Unity example, you can try to find a LÖVE library that implements that function, or write one yourself. I wouldn't have much trouble writing one. EDIT Done, see below.

For your stated purpose, you could easily do it with groups: just set your player and your bullets to group -1. If you want the enemies to shoot and not collide with their own bullets, you can set the enemies and their bullets to group -2. That will prevent the enemies from colliding with each other, though, which may or may not be desirable. If you still want enemies to collide with each other, you probably have to start using categories instead.


EDIT: Here's a small library to implement a function to ignore pairs of fixtures. Note it's untested. EDIT2: Tested now, fixed an issue.

Code: Select all

--[=======================================================================[--

Simple library to implement a function that allows causing two given fixtures
to not collide which each other.

Usage:

local world = love.physics.newWorld(...)
local ignoreCollision = require('stopCollision')(world)

...

ignoreCollision(fixture1, fixture2, true) -- add pair to ignore list
ignoreCollision(fixture1, fixture2, false) -- remove pair from ignore list

Written by Pedro Gimeno, donated to the public domain.

--]=======================================================================]--

local weak = {__mode = 'k'}

return function(world)
  local collisionPairs = setmetatable({}, weak)
  world:setContactFilter(function (fixt1, fixt2)
    return not (collisionPairs[fixt1] and collisionPairs[fixt1][fixt2]
             or collisionPairs[fixt2] and collisionPairs[fixt2][fixt1])
  end)
  return function(fixt1, fixt2, ignore)
    if ignore then
      -- Try to not duplicate pairs
      if collisionPairs[fixt1] and collisionPairs[fixt1][fixt2] then return end
      if collisionPairs[fixt2] then
        collisionPairs[fixt2][fixt1] = true
        return
      end
      if not collisionPairs[fixt1] then
        collisionPairs[fixt1] = setmetatable({}, weak)
      end
      collisionPairs[fixt1][fixt2] = true
      return
    end

    -- ignore is false - remove the entry
    if collisionPairs[fixt1] then
      collisionPairs[fixt1][fixt2] = nil
    end
    if collisionPairs[fixt2] then
      collisionPairs[fixt2][fixt1] = nil
    end
  end
end
Note that if you want to have more than one physics world, you need to have one ignoreCollision function per world, otherwise it won't find the fixtures.
Last edited by pgimeno on Tue Jul 03, 2018 4:59 pm, edited 1 time in total.
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by DarkShroom »

Hi

thanks for your replies, pgimeno's... i understand the information.... there was a "world:setContactFilter" function!!! :)

(jeeper, yes i was missing something)

that's basically what i want I recon, actually I might not even have to ask my next support thread question now! I was, and had, previously asked about a "circle cast" or like a "shape cast"... i imagine now i have the leads i need to get something like that, so i can customise the physics responses... and the principle looks very the same as the rest of how Love 2D works :)

i think soon I'm gonna have a demo of dynamically destroying physics shapes, i've finished now my polygon reduction code and also i have for a long time had a boolean library from github... well dynamic terrain destruction in love 2d i may be posting as an example soon

well that cheered me up, thanks, it was looking like oh crap i put all this time in and i can't really make at least the little tech demo i wanted to demonstrate.

Thanks very much guys, people trying to help here even when others are perhaps unspecific to a degree really means a lot.

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

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by DarkShroom »

my EASY :) solution so far:

i find love2d's "setSensor" makes a rather convenient and easy method for basic bullets (mind you not rebounding grenade type ones yet)

i had not considered this as in unity the closest thing in unity is called a "trigger" and I made an assumption it would behave the same... a "trigger" in unity is only activated if another objects runs into it, in Love 2D it appears the "sensor" doesn't have this issue... actually so far, Love 2D's physics have been much better

So the "sensor" covers most of my personal issues off the bat, including making the equivalent of a "sphere cast" from unity... i can easily now check if squares are occupied for my pathfinding, check if objects would collide before spawing them for my map editing etc... nice



anyway, the code you have given me should be useful but... can I please keep the question open for my future code because, i quote:

"
local ignoreCollision = require('stopCollision')(world)
"

okay I cannot find this library anywhere? this guy must have wrote a "stopCollision.lua" file somewhere?? i found a similar named feature in "bump" but I'm not using that library.... so, does anyone know where "stopCollision.lua" is from perhaps?

Anyway in the meantime at least, a large amount of my problems are covered, i can simply program the reactions to sensor collisions
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by DarkShroom »

edit: er oh i get it.... sorry i was not reading the lua :megagrin:

thanks for all information, future problems now sorted to, all completely closed!
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: firing a missile from my ship, is it possible to have an EASY function like Unity 3D

Post by pgimeno »

stopCollision.lua is supposed to be the name of the file for the code snipped posted above :nyu:

Of course you can name it as you prefer.
Post Reply

Who is online

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