How to make different kind of objects fall with same speed in Corona - coronasdk

I have different images as display objects and I want them to fall with same speed in physics. How can I do that?

The density of the object's body must be all the same
physics.addBody(object,"dynamic",{density = 1)
Try this piece of code
local object1 = display.newRect(0,0,math.random(10,100),math.random(10,100))
object1.x = 100
object1.y = 0
physics.addBody(object1,"dynamic",{bounce = 1, friction = 0, density = 1})
local object2 = display.newRect(0,0,math.random(10,100),math.random(10,100))
object2.x = 200
object2.y = 0
physics.addBody(object2,"dynamic",{bounce = 1, friction = 0, density = 1})
Random sizes but same speed because of the density.

Related

Stopping an object falling through another using Physics in SceneKit

Swift 5, iOS 14
I have a box, that I give a dynamic physical body too and a plane on which I drop said box since I set the box to be affected by gravity.
I set up collision and contacts tests and detect the fact that one hits the other.
But I what I want to do is stop the box falling though the plane. Even if I turn gravity off when they collide, the stupid box keeps going...
I also tried changing the type from dynamic to static on the box, no effect.
I also tried changing the velocity of the box to zero on collision, no effect.
box.physicsBody?.isAffectedByGravity = true
box.physicsBody?.friction = 0
box.physicsBody?.restitution = 1 //bounceness of the object
box.physicsBody?.angularDamping = 1 // rotationess
box.physicsBody = SCNPhysicsBody(type: .dynamic, shape:SCNPhysicsShape(geometry: targetGeometry, options:nil))
box.physicsBody?.mass = 0.01
box.physicsBody?.categoryBitMask = foodCategory;
box.physicsBody?.contactTestBitMask = heroCategory;
box.physicsBody?.collisionBitMask = 0;
And
let planeGeo = SCNPlane(width: 8, height: 8)
planeGeo.firstMaterial?.diffuse.contents = UIColor.blue
planeGeo.firstMaterial?.isDoubleSided = true
let planeNode = SCNNode(geometry: planeGeo)
planeNode.simdPosition = SIMD3(x: 0, y: -4, z: -4)
planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(45), y: 0, z: 0)
planeNode.physicsBody?.isAffectedByGravity = false
planeNode.physicsBody?.friction = 0
planeNode.physicsBody?.restitution = 0 //bounceness of the object
planeNode.physicsBody?.angularDamping = 1 // rotationess
planeNode.physicsBody = SCNPhysicsBody(type: .static, shape:SCNPhysicsShape(geometry: planeGeo, options:nil))
planeNode.physicsBody?.categoryBitMask = heroCategory;
planeNode.physicsBody?.contactTestBitMask = foodCategory;
planeNode.physicsBody?.collisionBitMask = 0;
scene.rootNode.addChildNode(planeNode)
What have I missed here?
Try to use binary notation for your Physics Categories like:
let BitmaskCollision = Int(1 << 2)
let BitmaskCollectable = Int(1 << 3)
let BitmaskEnemy = Int(1 << 4)
let BitmaskSuperCollision = Int(1 << 5)
let BitmaskWater = Int(1 << 6)
Avoid using the 0 for the Collision Mask. Try using Int(1 << 2)
You also must use the category Bitmasks from your box within the collision detection of the plane i.Ex. like so:
planeNode.physicsBody?.collisionBitMask = BitmaskCollision | foodCategory
(example with multiple masks)
or
box.physicsBody?.collisionBitMask = heroCategory

SimpleBlobDetector not recognizing the more obvious circles

I am using SimpleBlobDetector with the parameters specified below:
# Parameters
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = 1500
params.filterByCircularity = True
params.minCircularity = 0.5
params.filterByConvexity = True
params.minConvexity = 0.9
params.filterByInertia = True
params.minInertiaRatio = 0.7
params.minDistBetweenBlobs = 10
params.filterByColor = False
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector(params)
keypoints = detector.detect(dilated)
im_with_keypoints = cv2.drawKeypoints(dilated, keypoints, np.array([]), (0, 0, 255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Display result
cv2.imshow("Keypoints", im_with_keypoints)
This produces the result below. As you can see, some of the 'cleaner' circles are not detected whilst others which have rougher edges are detected. What could be the problem please?
If you add
params.maxArea = 10000
you get this image:
so I assume there's a default maximum, and you're exceeding it.

optimal thresholding for extract hand image

I am working on palm print identification by palm texture and geometry. i want to binarized hand image in preprocessing step for extracting geometry features like palm width and finger width.
i have used Gaussian filter for reduced noise and Otsu method for thresholding but I could not reach Optimal image! i was wondering if someone help me!
my database downloaded from "IIT Delhi Touch-less Palm print "
I=imread('hand.jpg');
h= fspecial('gaussian', 15,5);
s=imfilter(I,h,'symmetric');
q=graythresh(I)
BW=im2bw(I,q);
I have tried the following code and getting some promising result on your dropbox images. you can try it and share your results for further approach.
clc
clear all
close all
impath = 'E:\GoogleDrive\Mathworks\irisDEt\HandSeg';
[name,path] = uigetfile({'*.jpg';'*.png'},'mytitle',impath);
im =imread([path,name]);
im = imresize(im,0.5);
gms = 15;
red = im(:,:,1);
redmed = medfilt2(red,[gms,gms],'symmetric');
redmedbw = im2bw(redmed,0.9*graythresh(redmed));
redmedbw = bwareaopen(redmedbw,1500);
redmedbw = imclose(redmedbw,strel('disk',5));
figure,imshow(im,[])
figure,imshow(redmed,[])
figure,imshow(redmedbw,[])
My results are:
Code for signature estimation of the structure and extraction on critical(peaks and vallys) from the structure:
function [sig,xysamp,idx]= signature(bw,prec)
boundry = bwboundaries(bw);
xy = boundry{1};
x = xy(:,1);
y = xy(:,2);
len = length(x);
res = (len/prec);
re = rem(res,2);
if re
res = ceil(res);
end
indexes = 1:res:len;
xnew = x(indexes);
ynew = y(indexes);
xysamp = [xnew,ynew] ;
cx = round(mean(xnew));
cy = round(mean(ynew));
xn = abs(xnew-cx);
yn = abs(ynew-cy);
% ang = atand(yn./xn);
sig = (xn.^2+yn.^2);
sig = sig/max(sig);
% Critical Points in Signatures.
diffsig = diff(sig);
% pos = zeros(length(diffsig),1);
idx = 1;
for i = 2:length(diffsig)
if diffsig(i-1)*diffsig(i) <0
idx = [idx,i];
end
end
idx = [idx,i];
Here idx are the indexes of xysamp which gives actual boundry location in the image. the location of peaks and vallys may not be exact as i m doing sampling of boundry and it is a very simple way to approach the structural based problems.
Thank You
Result of critical point extraction:

Corona Vertices of a Hexagon

Ok, so I'm trying to create hexagons for my game. The first option I had is to have several images of hexagon, but I'm having problems with clickable area since these images are positioned side-by-side.
So i guess my only option is to create objects using polygons. Here is a code from corona sdk's website:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, }
local o = display.newPolygon( halfW, halfH, vertices )
o.fill = { type="image", filename="mountains.png" }
o.strokeWidth = 10
o:setStrokeColor( 1, 0, 0 )
That code is for creating a star. But I don't know how to create a hexagon using vertices.
Try this to create the vertices array:
local R = 45
local N = 6
local vertices = {}
local i = 0
for t = 0, 2*math.pi, 2*math.pi/N do
i=i+1; vertices[i]= R*math.cos(t)
i=i+1; vertices[i]= R*math.sin(t)
end
And this to draw the hexagon:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local hexagon = display.newPolygon( halfW, halfH, vertices )
hexagon.fill = { type="image", filename="mountains.png" }
hexagon.strokeWidth = 10
hexagon:setStrokeColor( 1, 0, 0 )
I chose R=45 to produce a polygon of the same size of your star.
You could always use a graphics.newMask() to apply a mask to each image hex that would make the outside area not touchable.

Corona SDK custom physics bodies

i have some trouble with the custom shapes in corona.
Here is my code and what it does is that i'm adding some spheres to the scene so that they fall inside a basket, this basket is the custom shape object that i defined in the newBasket() function, the problem is that the basket does collide with the ground object but it doesn't collide with the spheres and i don't know why, please someone help me here, i can't find a solution elsewhere, thanks in advance.
_W = display.contentWidth
_H = display.contentHeight
--Physics
local physics = require("physics")
physics.start()
physics.setDrawMode("debug")
-- iOS
display.setStatusBar(display.HiddenStatusBar)
-- screen boundaries
local ground = display.newRect(0, _H, _W, 5)
local leftWall = display.newRect(0,0,1,_H)
local rightWall = display.newRect(_W,0,1,_H)
physics.addBody(ground, "static", {friction = .2, bounce = .1})
physics.addBody(leftWall, "static", {friction = .2, bounce = .1})
physics.addBody(rightWall, "static", {friction = .2, bounce = .1})
local function newBasket()
local body = display.newImage("assets/basket.png")
body.x = 0 body.y = 0
local physics_body = {}
physics_body["basket"] = {
{
--LeftArm
density = 10, friction = 10, bounce = 0.15,
shape = {-126, 40, -110, 40, -140, -64, -156, -64}
},
{
--Bottom
density = 10, friction = 1, bounce = 0,
shape = {-121, 60, 125, 60, 128, 40, -126, 40}
},
{
--RightArm
density = 10, friction = 10, bounce = 0.15,
shape = {113, 40, 129, 40, 158, -64, 143, -64}
}
}
physics.addBody(body, unpack(physics_body["basket"]) )
return body
end
local basket = newBasket()
basket.x = _W * .5 basket.y = _H - 100
local function newPlanet()
local planets = {
{img = "bigBall.png", radius = 45},
{img = "mediumBall.png", radius = 30},
{img = "smallBall.png", radius = 20}
}
local n = math.random(1,3)
local img = "assets/" .. planets[n].img
local ball = display.newImage(img)
ball.x = math.random((_W * 0.5) -100, (_W * 0.5) + 100) ball.y = 0
physics.addBody(ball, {bounce = 0.3, radius = planets[n].radius})
end
local function spawnPlanets(number)
local function spawn(e)
newPlanet()
if(e.count == number) then
timer.cancel(tmr)
tmr = nil
end
end
tmr = timer.performWithDelay(500, spawn, number)
end
spawnPlanets(20)
According to the manual:
If a shape is specified, then the body boundaries will follow the polygon provided by the shape. Note that the maximum number of sides per shape is eight, and all angles must be convex. [...] The shape coordinates must be defined in clockwise order, and the resulting shape must be convex-only.
So you have two issues working against you. First the shapes must be defined in clockwise order, as I've done in the below example. Secondly, all shapes must be convex (even complex body shapes) so you can't do anything that turns in on itself like a crescent moon or basket.
Unfortunantly, this means you have to make your basket as 3 separate shapes. Also, since they are now separate shapes, they won't stay stuck together when they fall (unless you use joints). I've just made the basket 3 static bodies and put it in the right place to start with:
local function newBasket()
local physics_body = {}
physics_body["basket"] = {
{
--LeftArm
density = 10, friction = 10, bounce = 0.15,
shape = {-126, 40, -156, -64, -140, -64, -110, 40 }
},
{
--Bottom
density = 10, friction = 1, bounce = 0,
shape = {-121, 60,-126, 40, 128, 40, 125, 60 }
},
{
--RightArm
density = 10, friction = 10, bounce = 0.15,
shape = {113, 40, 143, -64, 158, -64, 129, 40 }
}
}
for k,shape in pairs(physics_body["basket"]) do
local body = display.newRect(0, 0, 200, 100)
body.x = display.contentCenterX
body.y = display.contentHeight - 60
physics.addBody(body, 'static', shape )
end
end
newBasket()

Resources