collision with enemy and player...who?

hi,
It´s a specific request…i would determine who collide with whome. For example my player go to my enemy and then a specific action. And next my enemy go to my player and then an another action.

The diificulty is that my player and my enemy could be in movement in the same time and the challenge is to determine who have the best trajectory to collide with the other.

I looked to precollision filter but i dońt believe that could resolve this. Could you help me ?

You’ll have to clarify what you mean by “best trajectory”. Would you mean like this; objectA travelling directly to right, objectB travelling directly up, if at collision objectB is directly under objectA, then it’s objectB’s hit, but if objectB is directly to the right, it’s objectA’s hit?

I hope that’s clear.

If that’s the case, then I think you’ll need to calculate four angles:

  1.  Angle of objectA’s velocity
  2.  Angle of objectB’s velocity
  3.  Angle of objectA to objectB
  4.  Angle of objectB to objectA

you can then compare for each object, the angle it’s travelling at, and the angle from it to the other object. The smaller the difference the more of a “direct hit” it is.

hi hasty,

you have fully understood what i want.

but there is still something that i don’t know.

how do you calculate the Angle of objectA’s velocity, it suppose that you have store the begin value (x and y) and the final value(x and y)…how do you do that ?

I hope that i’m clear…

thanks

If you’re using the physics library then do this:

local xVel, yVel = objectA:getLinearVelocity() local angle = math.atan2(yVel, xVel)

If you’re not using physics, then what method are you using to move your objects?

yes i use physics.

thanks i don’t know that i can do reverse object:getLinearVelocity(x,y) to this

local xVel, yVel = objectA:getLinearVelocity()

+one thing that i know…have a good day :slight_smile:

hi,

i have tried what you explain but my value xVel,yVel and angleV are nul …

I missed something ?

local physics = require("physics") physics.start() local character=display.newCircle(100,100,10) physics.addBody( character, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) character:applyForce( 0, -1800, character.x, character.y ) local xVel, yVel = character:getLinearVelocity() local angleV = math.atan2(yVel,xVel) local function testPosition (event)     print(xVel,yVel,angleV) end Runtime:addEventListener("enterFrame", testPosition)

 

 

Those values are not nil, it’s just that you only set them the once, presumably before the physics engine has converted the force applied into velocity. If you define xVel, yVel and angleV inside your testPosition function, it will print out the values you want to see.

ok thanks it works, but…

local physics = require("physics") physics.start() local objectA=display.newCircle(100,100,10) physics.addBody( objectA, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectA:applyForce( 50, 100, objectA.x, objectA.y ) local objectB=display.newCircle(150,100,10) physics.addBody( objectB, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectB:applyForce( -20, 100, objectB.x, objectB.y ) local function testPosition (event)     print(objectA.x,objectA.y,"positionObjectA")     print(objectB.x,objectB.y,"positionObjectB")     local xVelA, yVelA = objectA:getLinearVelocity()     local xVelB, yVelB = objectB:getLinearVelocity()     local angleA = math.atan2(yVelA,xVelA)     local angleB = math.atan2(yVelB,xVelB)     local angleAB=math.atan2(objectA.x-objectB.x,objectA.y-objectB.y)     local angleBA=math.atan2(objectB.x-objectA.x,objectB.y-objectA.y)     print(xVelA,yVelA,angleA,"velocity")     print(xVelB,yVelB,angleB,"velocity")     print(angleAB,angleBA) end Runtime:addEventListener("enterFrame", testPosition)

i think of another thing, if i compare the velocity value of the object A and B, i will find who hit in first the object.

if speedObjectA \> speedObjectB then print("objectA hit the objectB") else print("objectB hit the objectA") end

it’s more simple…no ?

That’s up to you. But it would not work for my original interpretation of what you’re trying to do. If objectA is moving from left to right, and objectB is moving directly up and hits the under side of objectA… “who hit” will be whoever is going faster, rather than whose hit was more “direct”. But if comparing speed difference works for what you’re trying to do then great, much simpler!

hi,

it’s sure your interpretation is best. here below the snippet. Is my snippet correct ?

local physics = require("physics") physics.start() local objectA=display.newCircle(500,100,10) objectA:setFillColor(1,1,0) physics.addBody( objectA, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectA:applyForce( 0, 100, objectA.x, objectA.y ) local objectB=display.newCircle(150,100,10) physics.addBody( objectB, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectB:applyForce( 80, 150, objectB.x, objectB.y ) function getAngle(x1,y1,x2,y2) &nbsp;&nbsp; &nbsp;local deltaY = y2 - y1 &nbsp;&nbsp; &nbsp;local deltaX = x2 - x1 &nbsp;&nbsp; &nbsp;local angleI= math.abs(math.atan2(deltaY, deltaX)) &nbsp;&nbsp; &nbsp;return angleI end local function testPosition (objectA,objectB) &nbsp;&nbsp; &nbsp;local xVelA, yVelA = objectA:getLinearVelocity() &nbsp;&nbsp; &nbsp;local xVelB, yVelB = objectB:getLinearVelocity() &nbsp;&nbsp; &nbsp;print("yVelA",yVelA, "xVelA",&nbsp; xVelA,"yVelB",yVelB,"xVelB", xVelB) &nbsp;&nbsp; &nbsp;print(objectA.x,objectA.y,"positionObjectA") &nbsp;&nbsp; &nbsp;print(objectB.x,objectB.y,"positionObjectB") &nbsp;&nbsp; &nbsp;local angleA =&nbsp; math.abs(math.atan2(yVelA,xVelA)) &nbsp;&nbsp; &nbsp;local angleB = math.abs(math.atan2(yVelB,xVelB)) &nbsp;&nbsp; &nbsp;local angleAtoB=getAngle(objectA.x,objectA.y,objectB.x,objectB.y) &nbsp;&nbsp; &nbsp;local angleBtoA=getAngle(objectB.x,objectB.y,objectA.x,objectA.y) &nbsp;&nbsp; &nbsp;print("angleA", angleA, "angleB", angleB) &nbsp;&nbsp; &nbsp;print("angleAtoB", angleAtoB, "angleBtoA", angleBtoA) if (math.abs(angleA-angleAtoB)) \< (math.abs(angleB-angleBtoA)) then &nbsp;&nbsp; &nbsp;print("objectAhitB") else &nbsp;&nbsp; &nbsp;print("objectBhitA") end end Runtime:addEventListener("enterFrame", function() testPosition(objectA,objectB) end)

Getting there.

I wouldn’t use math.abs on angleA and angleB. This would convert -1 radians to 1radian, which is a very different angle. To ensure that the angle is positive you could add TWOPI to it, where TWOPI = math.pi * 2. Then you can get modulo TWOPI of the result to ensure the angle isn’t above two pi radians (i.e. 360 degrees). You’d want to do the same for angleAtoB and angleBtoA.

Not too much, but it does leave one small problem… the situation where the difference in angles is very small, but each angle is either side of 0. e.g. 355 degrees, versus 5 degrees ( or whatever the radian equivalent would be). The real difference is 10 degrees, but

(math.abs(angleA-angleAtoB))

would give you 350 instead.

To solve this, you need a check for when the difference is greater than 1 radian ( 180 degrees - if the difference is over this, you know the actual difference must be less). Subtract TWOPI from the difference of the two, get the abs of it and use that as the new difference.

You’ll have to clarify what you mean by “best trajectory”. Would you mean like this; objectA travelling directly to right, objectB travelling directly up, if at collision objectB is directly under objectA, then it’s objectB’s hit, but if objectB is directly to the right, it’s objectA’s hit?

I hope that’s clear.

If that’s the case, then I think you’ll need to calculate four angles:

  1.  Angle of objectA’s velocity
  2.  Angle of objectB’s velocity
  3.  Angle of objectA to objectB
  4.  Angle of objectB to objectA

you can then compare for each object, the angle it’s travelling at, and the angle from it to the other object. The smaller the difference the more of a “direct hit” it is.

hi hasty,

you have fully understood what i want.

but there is still something that i don’t know.

how do you calculate the Angle of objectA’s velocity, it suppose that you have store the begin value (x and y) and the final value(x and y)…how do you do that ?

I hope that i’m clear…

thanks

If you’re using the physics library then do this:

local xVel, yVel = objectA:getLinearVelocity() local angle = math.atan2(yVel, xVel)

If you’re not using physics, then what method are you using to move your objects?

yes i use physics.

thanks i don’t know that i can do reverse object:getLinearVelocity(x,y) to this

local xVel, yVel = objectA:getLinearVelocity()

+one thing that i know…have a good day :slight_smile:

hi,

i have tried what you explain but my value xVel,yVel and angleV are nul …

I missed something ?

local physics = require("physics") physics.start() local character=display.newCircle(100,100,10) physics.addBody( character, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) character:applyForce( 0, -1800, character.x, character.y ) local xVel, yVel = character:getLinearVelocity() local angleV = math.atan2(yVel,xVel) local function testPosition (event) &nbsp;&nbsp; &nbsp;print(xVel,yVel,angleV) end Runtime:addEventListener("enterFrame", testPosition)

 

 

Those values are not nil, it’s just that you only set them the once, presumably before the physics engine has converted the force applied into velocity. If you define xVel, yVel and angleV inside your testPosition function, it will print out the values you want to see.

ok thanks it works, but…

local physics = require("physics") physics.start() local objectA=display.newCircle(100,100,10) physics.addBody( objectA, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectA:applyForce( 50, 100, objectA.x, objectA.y ) local objectB=display.newCircle(150,100,10) physics.addBody( objectB, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectB:applyForce( -20, 100, objectB.x, objectB.y ) local function testPosition (event) &nbsp;&nbsp; &nbsp;print(objectA.x,objectA.y,"positionObjectA") &nbsp;&nbsp; &nbsp;print(objectB.x,objectB.y,"positionObjectB") &nbsp;&nbsp; &nbsp;local xVelA, yVelA = objectA:getLinearVelocity() &nbsp;&nbsp; &nbsp;local xVelB, yVelB = objectB:getLinearVelocity() &nbsp;&nbsp; &nbsp;local angleA = math.atan2(yVelA,xVelA) &nbsp;&nbsp; &nbsp;local angleB = math.atan2(yVelB,xVelB) &nbsp;&nbsp; &nbsp;local angleAB=math.atan2(objectA.x-objectB.x,objectA.y-objectB.y) &nbsp;&nbsp; &nbsp;local angleBA=math.atan2(objectB.x-objectA.x,objectB.y-objectA.y) &nbsp;&nbsp; &nbsp;print(xVelA,yVelA,angleA,"velocity") &nbsp;&nbsp; &nbsp;print(xVelB,yVelB,angleB,"velocity") &nbsp;&nbsp; &nbsp;print(angleAB,angleBA) end Runtime:addEventListener("enterFrame", testPosition)

i think of another thing, if i compare the velocity value of the object A and B, i will find who hit in first the object.

if speedObjectA \> speedObjectB then print("objectA hit the objectB") else print("objectB hit the objectA") end

it’s more simple…no ?

That’s up to you. But it would not work for my original interpretation of what you’re trying to do. If objectA is moving from left to right, and objectB is moving directly up and hits the under side of objectA… “who hit” will be whoever is going faster, rather than whose hit was more “direct”. But if comparing speed difference works for what you’re trying to do then great, much simpler!

hi,

it’s sure your interpretation is best. here below the snippet. Is my snippet correct ?

local physics = require("physics") physics.start() local objectA=display.newCircle(500,100,10) objectA:setFillColor(1,1,0) physics.addBody( objectA, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectA:applyForce( 0, 100, objectA.x, objectA.y ) local objectB=display.newCircle(150,100,10) physics.addBody( objectB, { density=.5, friction=1, bounce=0.7,shape=pentagonShape } ) objectB:applyForce( 80, 150, objectB.x, objectB.y ) function getAngle(x1,y1,x2,y2) &nbsp;&nbsp; &nbsp;local deltaY = y2 - y1 &nbsp;&nbsp; &nbsp;local deltaX = x2 - x1 &nbsp;&nbsp; &nbsp;local angleI= math.abs(math.atan2(deltaY, deltaX)) &nbsp;&nbsp; &nbsp;return angleI end local function testPosition (objectA,objectB) &nbsp;&nbsp; &nbsp;local xVelA, yVelA = objectA:getLinearVelocity() &nbsp;&nbsp; &nbsp;local xVelB, yVelB = objectB:getLinearVelocity() &nbsp;&nbsp; &nbsp;print("yVelA",yVelA, "xVelA",&nbsp; xVelA,"yVelB",yVelB,"xVelB", xVelB) &nbsp;&nbsp; &nbsp;print(objectA.x,objectA.y,"positionObjectA") &nbsp;&nbsp; &nbsp;print(objectB.x,objectB.y,"positionObjectB") &nbsp;&nbsp; &nbsp;local angleA =&nbsp; math.abs(math.atan2(yVelA,xVelA)) &nbsp;&nbsp; &nbsp;local angleB = math.abs(math.atan2(yVelB,xVelB)) &nbsp;&nbsp; &nbsp;local angleAtoB=getAngle(objectA.x,objectA.y,objectB.x,objectB.y) &nbsp;&nbsp; &nbsp;local angleBtoA=getAngle(objectB.x,objectB.y,objectA.x,objectA.y) &nbsp;&nbsp; &nbsp;print("angleA", angleA, "angleB", angleB) &nbsp;&nbsp; &nbsp;print("angleAtoB", angleAtoB, "angleBtoA", angleBtoA) if (math.abs(angleA-angleAtoB)) \< (math.abs(angleB-angleBtoA)) then &nbsp;&nbsp; &nbsp;print("objectAhitB") else &nbsp;&nbsp; &nbsp;print("objectBhitA") end end Runtime:addEventListener("enterFrame", function() testPosition(objectA,objectB) end)