How do you make a sprite's "hitbox" smaller than it's bounds rectangle?

I have a game in which the player drags a circular-shaped sprite around the screen, avoiding square-shaped sprites that zip back and forth across the screen. However, I only want a collision to register when the "core" of this circular sprite collides with the "core" of one of these squares-shaped sprites. To do this, I am defining the "core" of my sprites using Spriteloqs new physics-oriented Shape Controls. This works great for defining the "cores" (or hit-boxes) of my sprites, but now, my sprites are bunching-up and bounding off each other when their hit-boxes overlap, which I don't want. I just want the sprites to pass through each other as if transparent, yet still register a collision within the system. Is there any way of making the bounds dimensions of your sprite smaller than it actually is without losing any of the sprite itself? Or a way to remove the physical "bodies" of the sprites while still retaining the collision detection properties of these bodies?

Removing the "physcial" bodies and still leaving collision detection sounds, possibly, like you want to use sensors?

If you mean you want the physics gone but the image to stay and still register collisions, then yes - sensors.

Is that the type of situation you're talking about? :)

Hi Peach,

Yup, I'm following the code sample Renjith provided in my previous thread dealing with collisions: https://developer.anscamobile.com/forum/2011/08/23/how-do-you-write-collision-detection-multiple-objects-same-name.
His method does use sensors, but I'm currently having to declare a physical body for my objects in order to get the "hit-boxes" I created for my sprites to be recognized. I explain it a bit better in the other thread:)

Thanks Peach,
Steven

Based on what you said in the other thread, you need to set isSensor on the sprite/physics body to true in response to the pre-collision event I think. If you do it in the collision event I'm guessing it'll still trigger a physics response.

Not sure.. try it out. :)

Thanks for your response, Don. I'm not sure exactly what you mean, however. Perhaps my explanation of my problem was a tad shoddy.

I'll clarify what I'm after: I've got square-shaped sprites within which I want to create smaller, rectangular "hit-boxes (sort of a soft, chewy centre that serves as the actual "body" of the sprite as pertains to collisions). However, I don't want these "bodies" to have physics effects (ie bound off each other). To begin (like you said), I use Spriteloq's new Shape Box tool to specify these rectangular "hit-boxes" within my squares, export, and then test using the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
local squareFactory = loqsprite.newFactory('sheet')
local circleFactory = loqsprite.newFactory('sheet')
local physics = require("physics")
physics.start()
physics.setGravity(0, 0)
physics.setDrawMode("hybrid")
 
local function squareSpawn (event)
  local square = squareFactory:newSpriteGroup()
  square:play('squareMovieClip')
  square.x = -100
  square.y = math.random(0,1024)
  physics.addBody(square,"dynamic",{isSensor = true})
  transition.to(square,  {time = 1500, delay = 0, x = square.x + 968, onComplete=function() square :removeSelf() end})
end
timer.performWithDelay(math.random(20,35),squareSpawn,0)
 
local circle = circleFactory:newSpriteGroup()
circle:play('circleMovieClip')
physics.addBody(circle,"kinematic")
function moveCircle( event )
  circle.x = event.x
  circle.y = event.y
end
Runtime:addEventListener("touch", moveCircle)
 
local function onCircleCollision()
  print("collided with square")
end
circle:addEventListener("collision",onCircleCollision)

I'm confused as to the effect you're trying to achieve. Sounds like you want physics and you don't at the same time. My guess is you don't want the square bodies to interact with square bodies, but you do want square bodies to react to other bodies.

If that's the chase you need to use the categoryBits, maskBits, or groupIndex parameters for your physics bodies. Seems like you might need to read up on the physics API: http://developer.anscamobile.com/content/game-edition-box2d-physics-engine

Btw, I updated the addPhysics call recently to pass on all parameters in the table used to specify the physics properties. So make sure you have a recent download of the API library.

Another note, you're only loading one sheet into a SpriteFactory. There's no reason to have two of them. Actually there's "never" a reason to have more than one factory unless you're going to dispose of them at different times.

In other words,

1
2
3
4
5
--local squareFactory = loqsprite.newFactory('sheet')
--local circleFactory = loqsprite.newFactory('sheet')
 
-- replace with 
local spriteFactory = loqsprite.newFactory('sheet')

Hi Don,

I don't want any physics effects in my game at all (ie I want all the bodies in my game to pass through each other as invisible). I just want the collisions between these bodies to register in the system (and only in the system) so they can trigger animations or sounds to play. It's sort of like having a tank full of amoeba passing through each other, and having a beeping sound play everytime their centres overlap/collide. They pass through one another without physics effects, but a collision is registered in the system that can trigger external effects. Does that make sense? If not, let me know.

You're better off asking for help in the general game development forum then. What you're trying to achieve has little to do with Spriteloq once you have your shapes. You'll need to figure out how to do collision detection with no physical response. I'm not sure if box2d is the right route.

If I were you, I'd look into box to box (aka AABB vs AABB) collision detection and avoid box2d all together.

Ok, perhaps I'm beating around the bush here. Let me cut to the chase. What I'd ideally like is customizable Bounds Rectangles. Currently, Spriteloq allows you to adjust the bounds rectangles of your sprites, but only for the purpose of cropping your sprite. In other words, if you are making a 2-d game like Street Fighter and you want to make the hit box of the characters slightly smaller than the character sprite itself, you cannot do this without Spriteloq cropping out the areas of your sprite outside of this hit-box. You can quite easily use an image editor to do any cropping of your sprite, so why not have Spriteloq default to capturing the whole sprite in it's entirety, and alter the purpose of the bounds rectangle to serve as a way of creating a sprite's "hit-box" or area of effect (within the game world). This would help immensely for people making 2-d games that don't require physics, such as myself. As it stands now, I'm stuck having to declare a physics shape, and wrestling with trying to get rid of it's physical properties, or, like you mentioned, coding an alternate solution outside box 2d.

Thanks,
Steven

The bounds rectangle is purely for the capture of pixel data. You could use that rectangle for collision detection with getRectShape. If you want a smaller box, use the shape tool to draw a box, and call getShapes()

They're documented here: http://www.loqheart.com/spriteloq/apidocs/files/loq_sprite-lua.html#SpriteGroup.Shape_Functions

If you're only using rectangles then AABB vs AABB detection would work for you.

Spriteloq doesn't support multiple shapes per frame, so you'd need custom tooling for a Street Fighter game.

Thanks Don, yes, my "hit-boxes" are all simple rectangles. I'll check out the documentation you mentioned and look into using getShapes(). The code I put-forward above already uses the bounds rectangle for collision, this is why it would be so great if I could customize my bounds rectangle without losing parts of my sprite.

Thank you,
Steven

views:2250 update:2011/10/12 9:17:43
corona forums © 2003-2011