Contacts Not Recognized When Body Is Changed from Circle to Rectangle

Sprite contact detection not working in swift

Before you can set properties for a node's physics body you first need to create one. There are volume based bodies such as a circle or rectangle and there are edge based physics bodies created from paths.

Seeing that you are currently working with balls, you would use a volume based body such as init(circleOfRadius:) or init(circleOfRadius:center:). It's best if you read up on the SKPhysicsBody class reference before proceeding.

Customize JointJS ports from circle to rectangle

We can customize the shapes of port.Following is the code sample for rectangular port , which was working for me.

ports: {
groups: {
'myPorts': {
position: 'top',
attrs: {
'.port-body': {
stroke: 'red',
strokeWidth: 2,
height: 10,
width: 10,
magnet: true
}
},
markup: '<rect class="port-body"/>'
}
},
items: [{
group: 'myPorts'
}],
}

D3 how to change a circle to rectangle on a mouseover

The simple answer is to follow what the answer in the other question mentioned https://stackoverflow.com/a/17439344/21061 however there is another way of doing this.

The tricky part is, SVG can't animate an svg:circle to an svg:square. It can however animate any svg:path into any other svg:path, and you can leverage this to actually produce a nice transition between a circle and a square. I've simplified the example from http://bl.ocks.org/mbostock/1020902 to demonstrate:



(function(){function n(n,a,r){for(var e,s,o,m=-1,u=2*Math.PI/a,b=0,h=[];++m<a;)e=n.m*(m*u-Math.PI)/4,e=Math.pow(Math.abs(Math.pow(Math.abs(Math.cos(e)/n.a),n.n2)+Math.pow(Math.abs(Math.sin(e)/n.b),n.n3)),-1/n.n1),e>b&&(b=e),h.push(e);for(b=r*Math.SQRT1_2/b,m=-1;++m<a;)s=(e=h[m]*b)*Math.cos(m*u),o=e*Math.sin(m*u),h[m]=[Math.abs(s)<1e-6?0:s,Math.abs(o)<1e-6?0:o];return t(h)+"Z"}var a=d3.svg.symbol(),t=d3.svg.line();d3.superformula=function(){function t(a,t){var u,b=r[e.call(this,a,t)];for(u in m)b[u]=m[u].call(this,a,t);return n(b,o.call(this,a,t),Math.sqrt(s.call(this,a,t)))}var e=a.type(),s=a.size(),o=s,m={};return t.type=function(n){return arguments.length?(e=d3.functor(n),t):e},t.param=function(n,a){return arguments.length<2?m[n]:(m[n]=d3.functor(a),t)},t.size=function(n){return arguments.length?(s=d3.functor(n),t):s},t.segments=function(n){return arguments.length?(o=d3.functor(n),t):o},t};var r={asterisk:{m:12,n1:.3,n2:0,n3:10,a:1,b:1},bean:{m:2,n1:1,n2:4,n3:8,a:1,b:1},butterfly:{m:3,n1:1,n2:6,n3:2,a:.6,b:1},circle:{m:4,n1:2,n2:2,n3:2,a:1,b:1},clover:{m:6,n1:.3,n2:0,n3:10,a:1,b:1},cloverFour:{m:8,n1:10,n2:-1,n3:-8,a:1,b:1},cross:{m:8,n1:1.3,n2:.01,n3:8,a:1,b:1},diamond:{m:4,n1:1,n2:1,n3:1,a:1,b:1},drop:{m:1,n1:.5,n2:.5,n3:.5,a:1,b:1},ellipse:{m:4,n1:2,n2:2,n3:2,a:9,b:6},gear:{m:19,n1:100,n2:50,n3:50,a:1,b:1},heart:{m:1,n1:.8,n2:1,n3:-8,a:1,b:.18},heptagon:{m:7,n1:1e3,n2:400,n3:400,a:1,b:1},hexagon:{m:6,n1:1e3,n2:400,n3:400,a:1,b:1},malteseCross:{m:8,n1:.9,n2:.1,n3:100,a:1,b:1},pentagon:{m:5,n1:1e3,n2:600,n3:600,a:1,b:1},rectangle:{m:4,n1:100,n2:100,n3:100,a:2,b:1},roundedStar:{m:5,n1:2,n2:7,n3:7,a:1,b:1},square:{m:4,n1:100,n2:100,n3:100,a:1,b:1},star:{m:5,n1:30,n2:100,n3:100,a:1,b:1},triangle:{m:3,n1:100,n2:200,n3:200,a:1,b:1}};d3.superformulaTypes=d3.keys(r)})();

var size = 1000;

var x = d3.scale.ordinal()

.domain(d3.superformulaTypes)

.rangePoints([0, 960], 1);

var svg = d3.select("body").append("svg")

.attr("width", 960)

.attr("height", 500);

var big = d3.superformula()

.type("square")

.size(size * 50)

.segments(360);

svg.append("path")

.attr("class", "big")

.attr("transform", "translate(450,250)")

.attr("d", big);

var circle = false;

setInterval(function() {

circle = !circle;

if(circle) {

d3.select(".big").transition().duration(500).attr("d", big.type("circle"));

} else {

d3.select(".big").transition().duration(500).attr("d", big.type("square"));

}

}, 3000);
path {

stroke-width: 1.5px;

}

.small {

fill: steelblue;

}

.big {

stroke: #666;

fill: #ddd;

}

.small:hover {

stroke: steelblue;

fill: lightsteelblue;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

SpriteKit Collision Detection

Try this code for your didBeginContact:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"bodyA:%@ bodyB:%@",contact.bodyA.node.name, contact.bodyB.node.name); // <- this gives you who touched who by object name

uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

if (collision == (towerCategory | creepCategory))
{
NSLog(@"we got contact...");
}
}

Remember that contact.bodyA.node.(property) can give you any property of SKNode. Things like name, position, xScale, yScale, alpha, children, parent and so on. You can see them all listed at the SKNode Class Reference.

CSS shape semicircle alignment problem with border of rectangle

You could add overflow hidden in case, and a full circle?

.ticket-outer {
overflow: hidden;
height: 150px;
width: 300px;
margin: 50px auto;
}

.ticket {
border: 5px solid #000;
border-radius: 20px;
height: 150px;
width: 300px;
position: relative;
box-sizing: border-box;
}

.ticket::before,
.ticket::after {
content: '';
width: 50px;
height: 50px;
border: 5px solid #000;
background: #fff;
border-radius: 50%;
position: absolute;
top: 50%;
left: -30px;
transform: translateY(-50%);
z-index: 2;
}

.ticket::after {
left: auto;
right: -30px;
}
<div class="ticket-outer">
<div class="ticket"></div>
</div>

How to followPath: in clockwise direction for rectangular shape path?

Short Answer: run the action in reverse to follow the path in the opposite direction

    [sprite runAction:[followTrack reversedAction]];

Long Answer: the followPath SKAction follows the direction in which the path was built. If you want your sprite to move along a rectangle path in a clockwise or counter-clockwise direction, then build the path accordingly. Alternatively, you can use the reverseAction method to follow a path in the opposite direction than it was built. Here are examples of both methods.

    BOOL clockwise = YES;
BOOL reversedActionMethod = NO;

CGRect rect = CGRectMake(CGRectGetWidth(self.frame)/2-50,CGRectGetHeight(self.frame)/2-50, 100, 100);
SKAction *followTrackCW = [SKAction followPath:[self clockwiseRectanglePathWithRect:rect] asOffset:NO orientToPath:YES duration:5];
SKAction *followTrackCCW = [SKAction followPath:[self counterClockwiseRectanglePathWithRect:rect] asOffset:NO orientToPath:YES duration:5];

if (!reversedActionMethod) {
if (clockwise) {
[sprite runAction:followTrackCW];
}
else {
[sprite runAction:followTrackCCW];
}
}
else {
if (clockwise) {
[sprite runAction:[followTrackCCW reversedAction]];
}
else {
[sprite runAction: followTrackCCW];
}
}

Build a rectangular-shaped path in clockwise order in scene coordinates (CCW in view coordinates)

- (CGPathRef) clockwiseRectanglePathWithRect:(CGRect)rect
{
CGFloat x = rect.origin.x;
CGFloat y = rect.origin.y;
CGFloat width = rect.size.width;
CGFloat height = rect.size.width;

UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(x, y)];
[bezierPath addLineToPoint: CGPointMake(x, y+height)];
[bezierPath addLineToPoint: CGPointMake(x+width, y+height)];
[bezierPath addLineToPoint: CGPointMake(x+width, y)];
[bezierPath closePath];
return bezierPath.CGPath;
}

Use built-in method to construct a rectangular-shaped path in counter-clockwise order in scene coordinates (and CW in view coordinates).

- (CGPathRef) counterClockwiseRectanglePathWithRect:(CGRect)rect
{
UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect:rect];
return bezierPath.CGPath;
}


Related Topics



Leave a reply



Submit