# Slick Forums

Discuss the Slick 2D Library
 It is currently Sun Jul 21, 2019 9:24 am

 All times are UTC

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: HollowCirclePosted: Wed Sep 12, 2007 2:13 pm

Joined: Mon Nov 13, 2006 3:06 pm
Posts: 42
Location: Argentina
I was needing a hollow circle that acts as an arena, so that other shapes can be either in or out, but not across it, so I implemented it, it's just an extension to Circle that redefines touches

In case someone finds it useful:

Code:
package net.phys2d.raw.shapes;

/**
* A hollow circle, only collides with another circles on the border
*
* @author Tulsi
*/
public strictfp class HollowCircle extends Circle {

/**
*/
}

/**
* Check if this circle touches another
*
* @param x
*            The x position of this circle
* @param y
*            The y position of this circle
* @param other
*            The other circle
* @param ox
*            The other circle's x position
* @param oy
*            The other circle's y position
* @return True if they touch
*/
public strictfp boolean touches(float x, float y, Circle other, float ox,
float oy) {
float dx = Math.abs(ox - x);
float dy = Math.abs(oy - y);

// outside
return false;
}

// inside
if (radDiff * radDiff >= dx * dx + dy * dy) {
return false;
}

// in between
}
}

The modified CircleCircleCollider:

Code:
package net.phys2d.raw.collide;

import net.phys2d.math.MathUtil;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.Contact;
import net.phys2d.raw.shapes.Circle;
import net.phys2d.raw.shapes.HollowCircle;

/**
* A collider for circle 2 circle collisions
*
* The create() method is used as a factory just in case this class becomes
* stateful eventually.
*
* @author Kevin Glass
*/
public strictfp class CircleCircleCollider implements Collider {
/**
* @see net.phys2d.raw.collide.Collider#collide(net.phys2d.raw.Contact[],
*      net.phys2d.raw.Body, net.phys2d.raw.Body)
*/
public int collide(Contact[] contacts, Body bodyA, Body bodyB) {
float x1 = bodyA.getPosition().getX();
float y1 = bodyA.getPosition().getY();
float x2 = bodyB.getPosition().getX();
float y2 = bodyB.getPosition().getY();

boolean touches = bodyA.getShape().getBounds().touches(x1, y1,
bodyB.getShape().getBounds(), x2, y2);
if (!touches) {
return 0;
}

Circle circleA = (Circle) bodyA.getShape();
Circle circleB = (Circle) bodyB.getShape();

touches = circleA.touches(x1, y1, circleB, x2, y2);
if (!touches) {
return 0;
}

Vector2f normal = MathUtil
.sub(bodyB.getPosition(), bodyA.getPosition());
- normal.length();
normal.normalise();

contacts[0].setPosition(pt);
if (sep < circleB.getRadius() || !(circleA instanceof HollowCircle)) {
contacts[0].setNormal(normal);
contacts[0].setSeparation(-sep);
} else {
// we're on the inside
contacts[0].setNormal(normal.negate());
}

FeaturePair fp = new FeaturePair();
contacts[0].setFeature(fp);

return 1;
}
}

And finally a HollowCircleDemo:

Code:
package net.phys2d.raw.test;

import net.phys2d.raw.Body;
import net.phys2d.raw.StaticBody;
import net.phys2d.raw.World;
import net.phys2d.raw.shapes.Circle;
import net.phys2d.raw.shapes.HollowCircle;

/**
* A simple demo with HollowCircle
*
* @author Tulsi
*/
public class HollowCircleDemo extends AbstractDemo {
/**
* Create the demo
*/
public HollowCircleDemo() {
super("Phys2D HollowCircle Demo");
}

/**
* @see net.phys2d.raw.test.AbstractDemo#init(net.phys2d.raw.World)
*/
protected void init(World world) {
Body body1 = new StaticBody("bounds", new HollowCircle(200));
body1.setPosition(250, 250);
body1.setRestitution(1);

Body body2 = new Body(new Circle(20), 10);
body2.setPosition(300, 200);
body2.setRestitution(1);

Body body3 = new Body(new Circle(5), 10);
body3.setPosition(200, 150);
body3.setRestitution(1);

Body body4 = new Body(new Circle(30), 10);
body4.setPosition(222, 111);
body4.setRestitution(1);
}

/**
* Entry point for tetsing
*
* @param argv
*            The arguments to the test
*/
public static void main(String[] argv) {
HollowCircleDemo demo = new HollowCircleDemo();
demo.start();
}
}

kev, jon or anybody, if you want a diff patch, just let me know

Top

 Post subject: Posted: Wed Sep 12, 2007 5:49 pm

Joined: Tue May 22, 2007 7:01 pm
Posts: 9
Instead of making it a stricly hollow circle it might be good for someone to create a torus shape instead.

http://en.wikipedia.org/wiki/Torus

Top

 Post subject: Posted: Wed Sep 12, 2007 6:05 pm

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Isn't a Torus a 3 dimensional shape? Do you mean a ring?

Kev

Top

 Post subject: Posted: Wed Sep 12, 2007 6:13 pm
 Oldbie

Joined: Tue Nov 28, 2006 6:18 pm
Posts: 429
I think he means a donut shape in which hollow circle is the special case where r1 and r2 are the same.

- Jon

Top

 Post subject: Posted: Wed Sep 12, 2007 7:47 pm

Joined: Tue May 22, 2007 7:01 pm
Posts: 9
Yea, what Jon said. I guess donut would be a better name for it since we arent 3D...

http://en.wikipedia.org/wiki/Doughnut

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ General    Games / Demos    Help / Discussion    Community Projects    Game Ideas/Design    Slick Competition 2011    Android Slick - LWJGL 2D Game Library    Announcements    Bug Reports / RFE    Extensions    Contribution depot Related Libraries and Tools    Phys2D    Tiled    TWL    Fizzy    Artemis Archive    Resolved Bugs/RFE    Useful Posts