In a physics-based CoronaSDK game I have a number of color balls. They all collide with each other. However I'm interested in those balls grouped together by their color.
So, each of the balls has a "color" property: ball.color = "red" for example.
In the listener for collisions I check if ball collided with the one with the same color:
local function ballCollision ( self, event )
local otherBall = event.other
if ( otherBall.color == self.color ) then
-- do some stuff here
if ( event.phase == "began" ) then
-- add the ball to the group
else
-- remove the ball from the group
end
end
end
ball.collision = ballCollision
ball:addEventListener ( "collision" )
Now, I was thinking about creating a global, module-wide, table of "groups", where I could keep a table of grouped balls. With each began phase of the collision I could add a ball colliding to the group the other collider belongs.
With each ended phase I could remove it from a group.
But this presents some (quite heavy, I think) calculations, for when the larger group is split to several smaller groups by one ball leaving it...
Is there some better solution to perform this? Like - getting a list of "chained" objects, or at least getting a list of colliders for each physics object?
Unfortunately I don't think you can avoid some kind of graph-traversal check when two balls separate, to find out whether that separation caused the group to become two groups, or whether the group remains intact because other parts of it still touching.
About getting a list of chained objects, the original Box2D (C++) has a function b2Body::GetContactList() which gives you a list of all other bodies currently in contact. You could use that for the graph-traversal check instead of keeping track of connectivity information yourself. I don't know whether Corona exposes that for you though...
If you are able to use that, keep in mind that it would give you all bodies in contact, so you would need to check again that the colors are matching as you traversed them. You should also check that IsTouching() is true for contacts because in Box2D the existence of a contact just means that the AABBs of two fixtures are overlapping.
If that function is not available, I guess you will need to maintain a connectivity graph yourself using the begin/end contact events.
I am assuming some kind of match-x or PuyoPuyo type of game here, in which case I doubt the number of balls involved would cause the processing to be too heavy. If you have hundreds of balls though, yeah, that could get slow.
Related
I got a few tower like things that a ball can jump on them.
The problem is that I can't make the towers move like +5 to left and still be collided.
Can someone help me?
CODE
_G.mainGroup = display.newGroup()
local rect=display.newRect( 200, 400, 100, 100 )
physics.addBody(rect, "static", {density= 3,friction= 0.2})
mainGroup:insert(rect)
-- timer.performWithDelay(1, function(e)
-- mainGroup.x = mainGroup.x - 4
-- end, 0 )
I tried this in my code. and the rectangle moves without collison with a ball but it leaves behind a invisible replica which has collision becoz a ball is able to bounce on it.
You should use kinematic objects instead of static objects. Static objects are used for objects which should stay stable for a long time. Kinematic objects can be stable in small times.
I'm working on a "Words in a pic" clone and I have different images representing each letters and empty boxes where the letters should be put in to.
When I drag the letters I want them to be dragged like when it is a static body i.e. just up, down, left and right (no turning or spinning) and when the item is within the box it should stay within that box, otherwise it should go back to it's original position.
The thing is that static objects can't collide with another static object nor can a kinematic object collide with another object so I need to use Dynamic if I have understood it correctly?
However how do I do so when the drag event is activated the body, the letter image, moves like a static or kinematic body (only up, down, left and right) but also detects collision between a letter image and a empty box image?
Thanks for helping me with this, I have not been able to find any information on how to solve this problem!
This was easier than I though, you set the items as "dynamic" and then object.isSensor = true, to make it not rotate object.isFixedRotation = true and also deactivate the gravity through object.gravityScale = 0
I am working on project where i need to find a path to the nearest (of many) goal node in 2D grid (8 connections in any node: N,S,W,E,NW,NE,SW,SE). There can be walls blocking path on the way. There is no problem when it comes to find path to one goal node, but how could i find in reasonable time which goal is nearest? I don't think running A* for every goal node and getting length of every path found to compare is reasonable way, how else can it be done?
Is A* somehow capable of finding path to nearest node, or the nearest goal must be found other way and then passed to A* as only goal?
Legend:
White - walkable
Grey - walls
Blue - myself
Green - goals
Red - enemy (i am planing to keep distance from enemy at X SQM)
Yes, just set EstimatedDistanceToEnd (aka. h(x)) to the minimum estimate over all end-nodes. Then just stop the search when you hit an end-node.
A* is for informed searches where we have some data other than just goal state... According to your question it seems like there is no relevant data and we just know what our goal state is. If that is the case then you can simply apply BFS.
I have a little logical problem over here.
As the title says, I try to build a boardgame as a computer-program (maybe with internet-support, but thats another story)
As for now, I have a map, which has some crossroads in it, hence I cannot simply define the fields as '1, 2, 3, 4, ...' because if there is a crossroad at field 10, I would have more than one field which has to be labeled 11 (Because then there is a field left and right of field 10, for example.)
So the problem is, if I cannot define the Board in numbers then I cannot simply get the possible positions a player can take when he rolls 2d6-dices with calculating 'Field-Nr. + RandomRange(1,6) + RandomRange(1,6)'
Does anybody have an idea, how to define a Map like this on another way, where I still can calculate the possible new-fields for Player X with a 2d6-dice-roll?
Thanks in advance.
If i understand well... (i don't thing so) this might help you. Just use dynamic arrays for your boardgame field and change your actions after the dimensions x,y .... Look at this "type Name = array of {array of ...} Base type; // Dynamic array"
It sounds like you have a graph of connected vertices. When a player is at a particular vertex of N edges, assuming N < 12, the new Field will be reached from traversing edge number N % ( rand(6) + rand(6) ).
You could also just do rand(12), but that would have an even distribution, unlike 2d6.
Instead of dynamic arrays, I would recommend using a linked-list of records to describe the surrounding cells, and traverse the player's location and possible moves using that linked-list.
First, define a record that describes each cell in your board's playable grid (the cells on the grid can be four-sided like a chessboard, or hexagonal like in Civilization V) ... each cell record should contain info such as coordinates, which players are also in that cell, any rewards/hazards/etc that would affect gameplay, etc. (you get the idea).
Finally, the linked-list joins all of these cells, effectively pointing to any connected cells. That way, all you'd need is the cell location of Player X and calculate possible moves over n amount of cells (determined by the dice roll), traversing the adjoining cells (that don't have hazards, for example).
If all you want is to track the possible roads, you can also use this approach to identify possible paths (instead of cells) Player X can travel on.
ok, here´s a first time noob question, sorry if that´s stupid.
I was just wondering, for a battleship kind of game, would it be a waste of memory to build a set of objects for each cell (10X10=100), with position(x,y) and state(empty,hit,missed) properties?
I was wondering if it would be better to only create an object Grid and use methods to calculate the cell positions whenever necessary (when handling cell selection with touches or drawing for example)..
The former is problematic because you may have ships that sit side-by-side or end to end and it will become difficult to know when one is completely destroyed just from the data structures you described. Two hits side-by-side could be two hits on the same ship, two hits to two different ships, or even a sinking for the smallest ship.
Go with the latter for sanity's sake.
If I was doing this, I would keep it simple, Have a 2 dimensional array, that's your 10 by 10 grid.
When someone takes a turn, calculate the position and;
if it's a miss, insert a 0 in that array cell
if it's a hit, insert a 1 in that array cell