Setting up Enemy Radar

HI dyson,

I am trying to setup an enemy with a ‘radar’ which is a circle to detect
when my player comes into range so that the enemy will start firing.

I create my sprite for my player, and then I make a circle.  I use
physics.newJoin to weld them together.  So I have  a ship with an invisible circle around it.

My problem is that when I touch scroll the map, I can start and stop the collisions, event though my player and the radar circle are out of range of each other.   Is this the best way to do contact with the enemy?

This is my setup code: :

local setupradar = {kind = “sprite”, layer =  mte.getSpriteLayer(1), locX
=params.itemx, locY =params.itemy, levelWidth = 256, levelHeight =256,}

local radar = display.newCircle(0,0,300)

radar.id = “radar”

mte.addSprite(radar, setupradar)

local setupenemy = {kind = “sprite”, layer =  mte.getSpriteLayer(1), locX
=params.itemx, locY =params.itemy, levelWidth = 64, levelHeight =64,}

mte.addSprite(object, setupenemy)

physics.addBody( radar,  “static”,  { density = 1, friction = 0, bounce =
0, radius=300, isSensor = true} )

physics.newJoint( “weld”, object, radar, params.itemx, params.itemy )

Thanks, Greg

It’s hard to get a handle on what’s going on without seeing it, but one thing that jumps out at me is how you’re using params.itemx and params.itemy. In your setup tables you use those values as map locations, but in your physics.newJoint you used them as world coordinates. They can’t be both. If your tiles are 32x32 pixels then location 1,1 is at position 16,16; location 2,1 is at position 48,16; and so on. 

If params.itemx and params.itemy define a location, you can convert them into a position as follows:

local posX, posY = mte.locToLevelPos(params.itemx, params.itemy)

If params.itemx and params.itemy define a position, you can convert them into a location as follows:

local locX, locY = mte.levelToLoc(params.itemx, params.itemy)

The physics.newJoint call requires a position, not a location.

Hi dyson,

When I make the radar circle visible, it is sitting right on top of the sprite, in its proper position.  If the position was wrong I would use loctolevelpos for sure.

Btw, should I be able to set the alpha of the circle with setFillColor? (my circles don’t seem to show alpha!)

        local setupradar = {kind = “sprite”, layer =  mte.getSpriteLayer(1), locX =params.itemx, locY =params.itemy,}
        radar = display.newCircle(0,0,256)
        radar:setFillColor( 0,0,0, .25)
–        radar:setStrokeColor( 1, 0, 0 )
        radar.id = “radar”
        radar.isVisible = false
        mte.addSprite(radar, setupradar)
 

Thanks, Greg

I would try it anyway. The position parameters of newJoint define where the two objects are linked. They’re positioned correctly over each other, but the actual physics connection could be way off in any direction. I haven’t used newJoint myself, so I’m not sure how the physics will react in that situation. When you add an object to a group, the bounds of the group increase to encompass it. Physics body may behave the same way in this situation. Perhaps adding the joint way off-center is allowing for the collision to trigger when the player touches it, even though the player isn’t touching the visible circle? Like I said, I’m not sure how it’ll come together.

Yes, you should be able to set the alpha of the fill using setFillColor and the alpha of the stroke using setStrokeColor. Try adding lighting = false to your setup table for the circle. That’ll rule out MTE’s lighting system overriding your alpha setting.

Hi dyson,

Your still batting 100!   lighting = false fixed the alpha, and with a tutorial from brent, I was able to combine my radar and sprite collision into one physics multibody with:

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } , {radius=256, isSensor = true})

Thanks again, Greg

It’s hard to get a handle on what’s going on without seeing it, but one thing that jumps out at me is how you’re using params.itemx and params.itemy. In your setup tables you use those values as map locations, but in your physics.newJoint you used them as world coordinates. They can’t be both. If your tiles are 32x32 pixels then location 1,1 is at position 16,16; location 2,1 is at position 48,16; and so on. 

If params.itemx and params.itemy define a location, you can convert them into a position as follows:

local posX, posY = mte.locToLevelPos(params.itemx, params.itemy)

If params.itemx and params.itemy define a position, you can convert them into a location as follows:

local locX, locY = mte.levelToLoc(params.itemx, params.itemy)

The physics.newJoint call requires a position, not a location.

Hi dyson,

When I make the radar circle visible, it is sitting right on top of the sprite, in its proper position.  If the position was wrong I would use loctolevelpos for sure.

Btw, should I be able to set the alpha of the circle with setFillColor? (my circles don’t seem to show alpha!)

        local setupradar = {kind = “sprite”, layer =  mte.getSpriteLayer(1), locX =params.itemx, locY =params.itemy,}
        radar = display.newCircle(0,0,256)
        radar:setFillColor( 0,0,0, .25)
–        radar:setStrokeColor( 1, 0, 0 )
        radar.id = “radar”
        radar.isVisible = false
        mte.addSprite(radar, setupradar)
 

Thanks, Greg

I would try it anyway. The position parameters of newJoint define where the two objects are linked. They’re positioned correctly over each other, but the actual physics connection could be way off in any direction. I haven’t used newJoint myself, so I’m not sure how the physics will react in that situation. When you add an object to a group, the bounds of the group increase to encompass it. Physics body may behave the same way in this situation. Perhaps adding the joint way off-center is allowing for the collision to trigger when the player touches it, even though the player isn’t touching the visible circle? Like I said, I’m not sure how it’ll come together.

Yes, you should be able to set the alpha of the fill using setFillColor and the alpha of the stroke using setStrokeColor. Try adding lighting = false to your setup table for the circle. That’ll rule out MTE’s lighting system overriding your alpha setting.

Hi dyson,

Your still batting 100!   lighting = false fixed the alpha, and with a tutorial from brent, I was able to combine my radar and sprite collision into one physics multibody with:

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } , {radius=256, isSensor = true})

Thanks again, Greg