I have created below hexagon structure in Andengine with Box2D physics engine. I want to rotate whole structure with respect to center when other ball collides with the structure using physics.
find reference image here : reference question
I tried weld joint and revolute joint with the bodies but it is not performing proper motion as required. All bodies are attached with weld joint and vertices have revolute joint with center body which is static like,
RevoluteJointDef revoluteJointDef1 = new RevoluteJointDef();
revoluteJointDef1.initialize(centerB, movingBody[i], centerB.getWorldCenter());
revoluteJointDef1.enableMotor = true;
revoluteJointDef1.motorSpeed = 0;
revoluteJointDef1.maxMotorTorque = 1f;
this.mPhysicsWorld.createJoint(revoluteJointDef1);
Is there any other way to perform smooth rotation of whole structure?
Thanks.
Oh! i got solution by myself... I did the same thing as i mentioned in first comment my question where I attached balls on a big circle body like,
Body circleBody ... ;// having large radius
//for all balls arranged in hexagon structure
foreach BallBody b
{
WeldJointDef def = new WeldJointDef();
def.initialize(b, circleBody, b.getWorldCenter());
mPhysicsWorld.createJoint(def);
}
and got the rotation by having revolute joint of circleBody with center. Thanks who put their efforts.
Related
I have a game world in which the player rotates around a point (like a planet). How can I rotate a body around a center point? I also want to somehow be able to use something like the actor classes interpolation for moving things, is that possible? Thanks!
You can rotate via vector2 easly.
Vector2 vectorbody = new Vector2(50f, 50f);
Vector2 vectorcenter = new Vector2(100f,100f);
Vector2 vectordis= vectorbody.cpy().sub(vectorcenter);//Displacement vector center to body
vectordis.setAngle( vectordis.angle() + rotatespeed );//Rotate speed can be negative that means it will rotates to other side.
vectordis.add(vectorcenter); //vectordis now became rotated vectorbody
vectorbody.set(vectordis); //vectorbody updated
You can also use actor methods.
Just define new variable like is_in_orbit and if its true (in orbit) then rotate otherwise move with a actor classes interpolation methods.
By the way you also have a opinion to use physics since Newton's law of universal gravitation is also physics but it will be complicated and can cause unexpected results in case of more center points (planets as you said) appear.
I'm currently working on my first Android game project using LibGdx. It is a 2D maze game where you use touch input to "draw" a line from one of the entrances to one of the exits. The world itself is a TiledMap, which act only as a visual backround at the moment.
The problem I have is the whole system of collision detection. There are (obliviously) walls in the maze, located on the edges of my background tiles. So when I slide my finger across a wall, the "player line" should stop at the wall. Additionally, events should be triggered when the line reaches an exit.
I could not find a way to properly implement these functionalities using the built-in libraries (I tried using Scene2D and Box2D). Stopping an actor's movement and firing an event is not that exotic, or?
All I need is some information on what I should use and maybe some first steps. :)
Thanks in advance!
Collision detection is probably the most difficult part of making a tiled game. Libgdx has a lot of useful methods to help you get the geometry but the actual collision handling after a collision is detected (called collision resolution) is a large topic. I suspect all you want is the player to stop. More advanced realistic solutions such as bouncing and friction are what Box2D specializes in.
First and foremost, getting the geometry that's going to collide.
a) The player can be represented by a rectangle. Have it extend the Sprite class and then you can use the getBoundingRectangle() function (A rectangle keeps things simple, but there are many other shapes that get used for collisions ).
b) Getting the geometry of the tiles, also a bunch of rectangles.
A function that gets the surrounding tiles.
public void getsurroundingTiles(int startX, int startY, int endX, int endY, Array<Rectangle> surroundingTiles){
TiledMapTileLayer layer = (TiledMapTileLayer) worldRef.getCurrentMap().getLayers().get("Terrain");
surroundingTiles.clear();
for (int y = startY; y <= endY; y++) {
for (int x = startX; x <= endX; x++) {
Cell cell = layer.getCell(x, y);
if (cell != null && cell.getTile().getProperties().containsKey("blocked")) {
Rectangle rect = new Rectangle();
rect.set(x, y, 1, 1);
surroundingTiles.add(rect);
}
}
}
}
}
This function gets the tiled map tile layer made in the Tiled program and fills up a Rectangle ArrayList with the rectangles only if the tile has the key "blocked".
So now you have rectangles representing your player, and representing all the
colliding blocks. You can draw them with a ShapeRenderer object.
This is what it looks like in a game I am working on.
Lastly, actually resolving the collisions is a larger topic. This is a great starting point.
http://www.metanetsoftware.com/technique/tutorialA.html
I am a newbie learner to JBox2D. I was just trying JBox2D for the first time on Android(I know Android development and I'm good in it) because my project needed physics.
Now, the tutorials and "Official User Manual" of Box2D said that negative gravity would result in objects being attracted downwards. But, in my case the object is being attracted upwards when I set Vec2's second parameter to be negative! Weird.
Here's the code which results in a circle shape going up on its own:
The Gravity:
Vec2 gravity = new Vec2(0.0f, -50.0f);
boolean doSleep = true;
world = new World(gravity, doSleep);
The circle shape is being made by following code:
//body definition
BodyDef bd = new BodyDef();
bd.position.set(200, 500);
bd.type = BodyType.DYNAMIC;
//define shape of the body.
CircleShape cs = new CircleShape();
cs.m_radius = 10f;
//define fixture of the body.
FixtureDef fd = new FixtureDef();
fd.shape = cs;
fd.density = 1f;
fd.friction = 0.2f;
fd.restitution = 0.8f;
//create the body and add fixture to it
body = world.createBody(bd);
body.createFixture(fd);
And I'm using SurfaceView canvas to draw:
canvas.drawCircle(body.getPosition().x, body.getPosition().y, 10, paint);
And stepp-ing as follows:
float timeStep = 1.0f / 60.f;
int velocityIterations = 6;
int positionIterations = 2;
world.step(timeStep, velocityIterations, positionIterations);
So, what's wrong within my code? I am unable to identify the mistake I've done.
Also,
I'm making a tennis-like 2D game on Android for which I'll be using JBox2D. So, can anybody tell me a tutorial/book on JBox2D? Though I googled vigorously, I couldn't find a good tutorial on it. (Though Box2D seems to be much popular instead of JBox2D)
I would be extremely grateful if someone could help me out here. Thank you.
In Box2D there is standard coordinate system: Y directed up, X to the right. In the graphic systems, usual, coordinate system has Y directed down, because window has static top-left corner. Looks like at your graphic system all the same. So, what in Box2D is moving down, you see as moving up.
It is the irritable problem, and directing gravity up is not the best solution. If you change only gravity, then you will need think about up-down problem in many other cases, for example, when define bodies, apply forces and so on. The most irritable, that it is not easy to understand, how physic coordinates conform graphic (for example, in one of my projects I had to draw points on paper, then turn paper back, rotate on 180 grades and look on the light :).
You can't change Box2D coordinate system, but, most likely, you can easy change coordinate system of graphic system by changing translation matrix. For example, in OpenGL it looks like this:
glScalef(1.0, -1.0, 1.0);
But take attention, after this, all that have positive Y coordinate will be not visible on the screen (it will be above the top edge of the window). So, you will need work with negative coordinates. If you don't want this, you can translate matrix down like this:
glTranslatef(0.0, -windowHeight.0, 0.0)
But before, think what to do if window would be resized.
About second question. I doubt whether you can find anywhere tutorial or book for JBox2D. JBox2D is port of Box2D (that means, it is exact copy of Box2D), and writing special book for it looks strange. Learn Box2D, and you will have no problem with JBox2D. For example, you can look there.
In Android I use a SurfaceView to display a simple 2D game. The bitmaps (.png) with alpha (representing the game objects) are drawn on the canvas.
Now I would like to do a simple but accurate collision detection. Checking whether these bitmaps are overlapping is quite easy.
But how do I check for collisions when these bitmaps have transparent areas? My challenge is detecting whether two balls collide or not. They fill up the whole bitmap in width and height both but in all four edges, there are transparent areas of course as it's a circle in a square.
What is the easiest way to detect collisions there only if the balls really collide, not their surrounding bitmap box?
Do I have to store coordinates of as many points on the ball's outline as possible? Or can Android "ignore" the alpha-channel when checking for collisions?
Another method I can think of will work with simple objects that can be constructed using Paths.
Once you have two objects whose boundaries are represented by paths, you may try this:
Path path1 = new Path();
path1.addCircle(10, 10, 4, Path.Direction.CW);
Path path2 = new Path();
path2.addCircle(15, 15, 8, Path.Direction.CW);
Region region1 = new Region();
region1.setPath(path1, clip);
Region region2 = new Region();
region2.setPath(path2, clip);
if (!region1.quickReject(region2) && region1.op(region2, Region.Op.INTERSECT)) {
// Collision!
}
Once you have your objects as Paths, you can draw them directly using drawPath(). You can also perform movement by transform()ing the path.
If it is ball collision you can perform analitical collision detection - it will be much faster then per-pixel detection. You only need to have two centers of balls (x1,y1) and (x2,y2) and radius r1 for the first ball and r2 for second one. Now if distance between centers of ball is less or equal of sum of radius then the balls are colliding:
colide = sqrt((x1-x2)^2+(y1-y2)^2)<=r1+r2
but a little faster way is to compare square of this value:
colide = (x1-x2)^2+(y1-y2)^2<=(r1+r2)^2
It's much easier to use an existing library like AndEngine instead of reinventing the wheel. I'm not sure if it can be used with a SurfaceView though. Check this article: Pixel Perfect Collision Detection for AndEngine.
Iam new to andengine. I wish to make a game which about football how to angle the ball in different directions and move it in an parabolic shape, i badly need an answer from any of you developers.
Thanks in advance if any one responds to my query.
you probably need to use the box2d physics engine plugin to do that, check out tutorials for using that and you should be able to do so
The direction of movement can be expressed as a two numbers
This vector can be converted into an angle.
Rotate your graphics to that angle.
Code example:
public Double rotateTowards(x1:Float, y1:Float) {
var radianAngle:Number = Math.atan2(x1, y1);
return radianAngle * 57.2957795;
}
This function returns the angle the football should rotate to face while moving in the x and y directions given. It will work with negative values as well. The code gives the rotation in radians. So you have to multiply it by 57.2957795 to convert it to degrees used by andengine's setRotation();
1 radians = 57.2957795 degrees
It is possible that your football may be rotating 90 degrees from the intended angle. If that is the case just add or subtract 90 to the value returned.
Do you still need help with the parabola or do you have that handled with box2D?