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.
Related
I'm trying to make a small circle move in another bigger circle as a ball moving in circle relative to Android phone tilting. I'm doing this in Godot but I think the principle is the same in all game engines probably. I make a scene in Godot and add two sprites as the two circles as the following picture. I get the accelerometer 3D vector, use x and y values and calculate the angle in x and y plate (relative to y axis i.e Vector2(0, 1) ) and rotate the mentioned scene to that degree using an animation, using this code
func _process(delta: float) -> void:
var vec3 = Input.get_accelerometer()
accelVec = Vector2(-stepify(vec3.x, 0.1), -stepify(vec3.y, 0.1))
var angle = accelVec.angle_to(Vector2(0, 1))
rotateTween.interpolate_property(self, "rotation", rotation, angle, 0.2,
Tween.TRANS_LINEAR)
rotateTween.start()
return
But the problem lies in here that when the x value of accelerometer 3D vector changes from a positive to negative value i.e when the ball is at top of the circle and is going to go to the other half of the circle, it actually moves from bottom of the circle to the desired point rather than from top of the circle.
I hope I explained the problem well, though I doubt it.
I added the Gif that shows actual test on an android phone here Testing in Android.
Any idea how to solve this problem?
Thanks in advance.
This is because Tween is interpolating linear values. It doesn't know it's working with angles, or that they should wrap around.
So when you're at -179 degrees and you tween to 179--a difference of 2 degrees--Tween just sees -179 -> 179 and goes through the whole circle.
You probably don't need a Tween here at all because _process() happens every frame.
I would like to convert 2d Screen Coordinates to 3d World Coordinates.
I put together a hack but would like a solid solution. If possible please use your equation/algorithm on the example below (if there is already a link, could you use the link to show the solution to the problem below)
My Environment is Java/C++ for Android and iOS using OpenGL 2.0
Problem to Solve
Where screen
(0,0) is top left and bottom right is screenWidth and screenHeight.
screenWidth=667; screenHeight=375;
//Camera's position,where camera is looking at, and camera direction
CamPos (0.0f,0.0f,11.0f); CamLookAt (0.0f, 0.0f, 10.5f); CamDir(0.0f,1.0f,0.0f);
//The Frustum
left =1.3f; right=1.3f; bottom=1.0f; top=1.0f; near=3.0f; far=1000.0f;
Obj
//object's position in 3d space x,y,z and scaling
objPos(1.0f, -.5f, 5.0f); objScale(.1f,.1f,0.0f);
The problem is how to convert (600,200) screen coordinates (with scaling of (.1f,.1f.0.0f))to the object's (1.0f,-.5f,5.0f) world coordinates to check if they are colliding(I am doing simple box collision, just need the 2d screen coordinates converted to world). I will put the hack that I have below but I am sure there is a better answer. Thank you in advance for any help. Please use the numbers above to show how your algorithm works.
Hack
//this is 0,0 in screen coordinates//-Cam_Offset =hardcoded to camera's pos
float convertedZeroY= (((mCam->right)*(Cam_Offset))); //2
float convertedZeroX= -(((Cam_Offset)+convertedZeroY));
convertedZeroY=convertedZeroY/2;
convertedZeroX=convertedZeroX/2;
//this is the length from top to bottom in game coordinates
float normX=convertedZeroX*2;
float normY=convertedZeroY*2;
//this is the screen coordinates converted to 3d world coordinates
mInput->mUserInputConverted->mPos.x=(-(((mInput->x)/mCam->mScreenW)*normX)+convertedZeroX);
mInput->mUserInputConverted->mPos.y=(-(((mInput->y)/mCam->mScreenH)*(normY))+convertedZeroY);
I just started experimenting libgdx and understanding... I looked sample projects... My problem :
The 1 and 6 originial ball number. And other balls, the ball's(1 and 6) will go randomly other places. (speed 1). ex . If a i am torch on the any ball, its speed up to 3...
The GameObjects should be in while loop. Ball images sometimes (randomly), the balls should be retun own 360 degrees. And get picture on TectureRegion.
Is there a similar example ? or
How can I do this ?
(Sorry for bad english)
Thanks...
As much as i understood you want your ball objects to move arround until you quit the game. Also you want to speed them up on touch right? Also you want to texture them and maybe they should detect collision with the screen borders and other balls to?
Libgdx has a main loop. This loop calls render(delta) every renderloop. The delta depends on the time ellapsed since last call of render. So on fast devices this delta is smaller then on slow devices (most times). This time is given in seconds. To move your objects you can add a value to their position in every render loop. In your case you want to add 1 (hopefully not pixel, as it then would seem slower on big screens):
for(BallObject ball : ballObjects) {
ball.setPositionX(ball.getPositionX() + ball.getSpeed() * delta * direction.x);
ball.setPositionY(ball.getPositionY() + ball.getSpeed() * delta * direction.y);
}
In this case a BallObject has a positionX and positionY describing his current position, a direction.x and direction.y, describing his movement in x and y direction (for 45° it would be direction.x=0.5 and direction.y=0.5), as well as a speed describing movement per second. This speed will be set to 3 on touch.
To speed the ball up on touch, you first need to implement InputProcessor in the class, which manages the movement of all ballobjects. Next you have to set it as the InputProcessor of the game: Gdx.input.setInputProcessor(this);. The InputProcessor has a method touchDown(int x, int y) or something like that. The x and y value are giving the coordinates in pixels, on the screen.
If you are using a camera or viewport in the new Libgdx version (you should do that) you have to use camera.unproject(x,y) or the viewport version of that (idk the exact method name). This method gives you the touchposition in your world coordinate system. Then you can simply check which ball is on this touchpos and set its speed to 3.
To texture the ball you have to use SpriteBatch for drawing. Look at the different draw() methods in the API and use the one which fits best for you. Just load a Texture, which should be a ".png" with a circle texture and the rest of it should be transparent (alpha = 0). With blending enabled (default) it will then only render the ball, even if it is actually a rectangle shaped Texture.
I hope it helps
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.
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?