Torque in Bullet - ogre

I'm working on a game which involves spaceships. We're using OGRE and Bullet.
The player's ship is supposed to pitch and yaw with the mouse -- moving the mouse left will yaw the ship left, for example. The problem is, I can't figure out how to apply torque in Bullet.
Before we started integrating Bullet, our solution was to directly modify the OGRE scene node like so (using OIS for input):
bool Game::mouseMoved(const OIS::MouseEvent &event) {
shipNode->yaw(Ogre::Degree(-event.state.X.rel));
shipNode->pitch(Ogre::Degree(event.state.Y.rel));
return true;
}
And that worked fine.
Now that we're using Bullet, my plan was to apply torque to the rigid body, like so:
bool Game::mouseMoved(const OIS::MouseEvent &event) {
//shipNode->yaw(Ogre::Degree(-event.state.X.rel));
//shipNode->pitch(Ogre::Degree(event.state.Y.rel));
shipRigidBody->applyTorqueImpulse(btVector3(-event.state.X.rel, event.state.Y.rel, 0));
return true;
}
Then, after stepping the simulation, update the scene node's orientation, like so:
void Game::update(const Ogre::FrameEvent &event) {
// ... snip ...
physicsWorld->stepSimulation(event.timeSinceLastFrame, 10);
btTransform shipTransform;
shipMotionState->getWorldTransform(shipTransform);
shipNode->setPosition(shipTransform.getOrigin().x(), /* ... snip ... */);
shipNode->setOrientation(shipTransform.getRotation().getAngle(), shipTransform.getRotation().getAxis().x(), /* ... snip ... */);
}
But with this setup, I can't orient the ship at all.
Next, I tried just applying a torque impulse of (100, 10, 200) every frame no matter what, then printing out shipTransform.getRotation().getAngle()). Always 0. At this point, I became extremely confused, since you'd think that always applying torque would make the body's orientation change.
So, my question is: Am I missing something stupid about btRigidBody::applyTorqueImpulse()? Or am I barking up the wrong tree entirely?
PS: shipRigidBody is constructed like this:
shipMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 100, 0)));
shipShape = new btBoxShape(btVector3(50, 20, 75));
shipShape->calculateLocalInertia(btScalar(1), btVector3(0, 0, 0));
shipRigidBody = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, shipMotionState, shipShape));
I also have code which accelerates the ship forwards or backwards based on keyboard input, and that code (which uses btRigidBody::applyCentralImpulse()) is working fine.

Related

Running background in P5

Im trying to make a side-scroller game and I got stuck on the running background part. I've looked for solutions and I've discovered some , but they were using javascript not the p5 library.
I started from the tutorials found on The Coding Train , and looked over all the examples and references on their site.
Although I could avoid this by using something else, just for the sake of it being here in case someone gets stuck on the same issue, could anyone offer a solution to this in p5? Disclaimer: Im a total noob p5.js.
later edit : By running background i mean moving Background image in a loop from left to right
Honestly, from the discussion we had in the comments, it sounds like you're overthinking it.
The general approach to animation (that tutorial is for Processing, but the principles apply to P5.js as well) is as follows:
Step 1: Create a set of variables that represent the state of your scene.
Step 2: Use those variables to draw your scene every frame.
Step 3: Change those variables over time to make your scene move.
You already know what to do: load an image that contains your background, then draw that image, and move it a little bit each frame.
You've said you want to call the background() function instead of the image() function, which doesn't make a ton of sense. The background() function is not any more efficient than the image() function. In fact, the background() function just calls the image() function for you!
From the P5.js source:
p5.prototype.background = function() {
if (arguments[0] instanceof p5.Image) {
this.image(arguments[0], 0, 0, this.width, this.height);
} else {
this._renderer.background.apply(this._renderer, arguments);
}
return this;
};
P5.js simply checks whether the argument is an image, and if so, calls the image() function for you. So it doesn't really make sense to say that using the image() function is "less efficient" than using the background() function.
Taking a step back, you should really avoid thinking about these kinds of micro-optimizations until you A: understand the problem and B: actually have a problem. Don't make assumptions about "efficiency" until you've actually measured your code for performance.
Anyway, back to your question. You also said that you're loading the image twice, which you shouldn't have to do. You can just load the image once (make sure you do that in the setup() function and not the draw() function, and then draw that image twice:
var img;
function preload() {
img = loadImage("image.jpg");
}
function setup() {
image(img, 0, 0);
image(img, 100, 100);
}
And since you can draw two images, you'd then just draw them next to each other. Here's an example using colored rectangles to show the approach more clearly:
var offsetX = 0;
function setup() {
createCanvas(200, 200);
}
function draw() {
background(0);
fill(0, 255, 0);
rect(offsetX, 0, width, height);
fill(0, 0, 255);
rect(offsetX + width, 0, width, height);
offsetX--;
if(offsetX <= -width){
offsetX = 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js"></script>
There are other ways to do it, like creating an image that contains the wrapping itself. But the general approach is pretty much the same.
If you're still stuck, please try to break your problem down into smaller pieces like I've done here. For example, notice that I created a simple sketch that deals with images, and another simple sketch that deals with moving rectangles. Then if you get stuck, please post a MCVE in a new question post and we'll go from there. Good luck.
Maybe it is a late answer.. but you can make the environment 3D and then move the camera.
Docs: https://p5js.org/reference/#/p5/camera
Example:
function setup() {
createCanvas(windowWidth - 200, windowHeight - 200, WEBGL);
background(175);
frameRate(30);
}
function draw() {
background(175);
//move the camera Xaxis when mouse is moved
let camX = map(mouseX, 0, width, 0,width);
camera(camX, 0, (height/2.0) / tan(PI*30.0 / 180.0), camX, 0, 0, 0, 1, 0);
normalMaterial();
noStroke();
ambientLight(251,45,43);
box(100, 100, 50);
ang += 0.3;
rotateY(ang * 0.03);
}
Keep calm and Happy Coding!

Unity3D: How do I improve my Idle animation (Y-axis)

Okay, I am almost finished with my 2d rpg click to move game. If you look at this video you will be able to see that when I click forward my player, once it gets to it's position, face the wrong direction rather than facing straight towards the players. To make myself more clearer, this is an image of the sprite sheet I am currently using, as you can see it has 8 directions. When you click here (in the game), my player would walk down and face this direction or this direction, rather than facing this direction (the normal/preferred position). This also happens when I click here (in the game) my player would walk up and face this direction or face this direction, rather than facing this direction. How can I make sure that my player face the right direction once it reached it's destination. Again this is only occurring within the Y-axis, so walking along the X-axis is fine.
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
var movementDirection = (target - transform.position).normalized;
Vector3 animDirection = Vector3.zero;
if (movementDirection.sqrMagnitude > 0)
{
// Use >= to default to horizontal on both being equal
if (movementDirection.x > movementDirection.y)
animDirection.x = 1;
else
animDirection.y = 1;
anim.SetBool ("walking", true);
anim.SetFloat ("SpeedX", movementDirection.x);
anim.SetFloat ("SpeedY", movementDirection.y);
if (movementDirection.x < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else if (movementDirection.x > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else {
anim.SetFloat ("LastMoveX", 0f);
}
if (movementDirection.y > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (movementDirection.y < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
}
} else {
if (Mathf.Approximately (transform.position.x, target.x) && Mathf.Approximately (transform.position.y, target.y)) {
touched = false;
anim.SetBool ("walking", false);
} else {
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
}
}
}
I'm just starting out with Unity, but hope I can help.
From the video you've posted I've noticed that the sprite is only 'wrong' when the oldPosition and newPosition click differs in the X component, e.g. when you would click straight down it would show the desired behavior and sprite.
Have you tried printing out the x and y values that your code is using to calculate which sprite it's setting?
At first I thought that maybe it's because the values you set in the Blend Tree were at -1, 1 etc, and due to normalizing you sometimes wound up with 0.9 for a certain value.
Can you maybe try debugging it with the animator window open, like you did at the end? Writing down the values and comparing them between Desired and Undesired behavior might tell you something more.
Sorry I don't have a concrete solution to your problem, but I hope this helps.
Edit for clarification:
Basically, what I'm recommending is to:
1) Print out the values you are getting when the behavior happens, an example of how to print these things out is by using LogFormat, for example:
Debug.LogFormat("X: {0}, Y: {1}", xPosition, yPosition);
Now, you will get the values printed out when it finishes moving.
2) Write down the values for when the moving finishes with the 'Wrong' sprite, and keep clicking to move until the 'Right' sprite shows up. Write down the values again.
3) Compare each values, and find the differences. Now deduce why the differences are as they are, using the values in conjunction with your blend trees.
4) Once you know why, go back through your code/blend trees and rewrite/fix it to work as you intended it to.

How to give control back to animation in Unity3D once altering objects location

I have a character made of up child objects that are animated using Unity3D's animation system.
While the player is walking, I can programmatically move the hand object up to catch a ball using the following code.
hand.position.y = ball.transform.position.y;
I need the hand object to go back to following the walk animation after it touches the ball, but instead it just stays at the exact position since it was set.
You want to use inverse kinematics and let Unity do the work of figuring out positioning for you. Here's a quick-and-dirty (untested) example for catching a ball (it's in C#, but it should be pretty similar for UnityScript):
// in a script on the same GameObject as your animation controller
bool isCatching;
Transform ball;
void OnAnimatorIK (int layer) {
if (isCatching) {
// set position and rotation weights for your catching hand
animator.SetIKPosition(AvatarIKGoal.RightHand, ball.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, ball.rotation);
} else {
// return your position and rotation weights back to their defaults (probably 0f?)
}
}
You'll need to do some work (possibly raycasting or just checking distance and direction) to determine when to set the isCatching flag to true, and you'll want to play with the weights for position and rotation to make it look natural. The IK manual entry has more detailed information.

How to use Transformation Matrix for SpriteBatch correctly?

I'm new to XNA and would like to develop a light-weight 2D engine over it, with the entities organized into parent-child hierarchy. I think of matrix when drawing children, because their position, rotation and scale are depend on their parent.
If I use SpriteBatch.Begin(), my rectangles can be drawn on the screen, but when I change them into:
this.DrawingMatrix = Matrix.Identity;
this.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullClockwise, null, this.DrawingMatrix);
nothing is drawn anymore. I even tried new Matrix() or Matrix.CreateTranslation(0, 0, 0) for DrawingMatrix.
My first question is: why doesn't it work? I'm not working with any camera or viewport.
Secondly, before drawing an entity, I call the PreDraw to transform the matrix (I will then reset to original state at PostDraw):
protected virtual void PreDraw(Engine pEngine)
{
pEngine.DrawingMatrix *=
Matrix.CreateTranslation(this.X, this.Y, 0) *
Matrix.CreateScale(this.ScaleX, this.ScaleY, 1) *
Matrix.CreateRotationZ(this.Rotation);
}
Please clarify the correction of above code. And I need to scale not at the origin, but at ScaleCenterX and ScaleCenterY, how can I achieve this?
ADDED: Here is an example of my engine's draw process:
Call these code:
this.DrawingMatrix = Matrix.CreateTranslation(0, 0, 0);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullClockwise, null, this.DrawingMatrix);
Call PreDraw(), with is:
protected virtual void PreDraw(Engine pEngine)
{
pEngine.DrawingMatrix *=
Matrix.CreateTranslation(this.X, this.Y, 0) *
Matrix.CreateScale(this.ScaleX, this.ScaleY, 1) *
Matrix.CreateRotationZ(this.Rotation);
}
Call Draw(), for example, in my Rect class:
protected override void Draw(Engine pEngine)
{
pEngine.SpriteBatch.Draw(pEngine.RectangleTexture, new Rectangle(0, 0, (int)this.Width, (int)this.Height), new Rectangle(0, 0, 1, 1), this.Color);
}
If I replace above Begin code with this.SpriteBatch.Begin(), the rectangle is drawn correctly, so I guess it is because of the matrix.
First issue is a simple bug: The default for SpriteBatch is CullCounterClockwise, but you have specified CullClockwise causing all your sprites to get back-face-culled. You can pass null if you just want to use the default render states - you don't need to specify them explicitly.
(You would need to change the cull mode if you used a negative scale.)
To answer your second question: You need to translate "back" to place the scaling origin (your ScaleCenterX and ScaleCenterY) at the world origin (0,0). Transformations always happen around (0,0). So normally the order is: translate sprite origin back to the world origin, scale, rotate, translate to place sprite origin at desired world position.
Also, I hope that your PostDraw is not applying the reverse transformations (you made it sound like it does). That is very likely to cause precision problems. You should save and restore the matrix instead.

Unity 3D Spinning a gameobject

I am trying to spin a 3D Gameobject in unity. It is actually a cylinder with just a texture of a poker chip on it. I want to spin it 360 degrees once it has collided with a raycast. It is working just fine in the Unity emulator, however, on the device itself the chip comes to a stop after its spin and then continues to spin in an endless loop. Here is a snippet of the code in question. Thank you for any help in advance.
// Spin the chip
if (Animate) {
if (Speed > 0 && Chip.tag.Contains("Chip")) {
Chip.transform.Rotate(0, Speed*Time.deltaTime, 0);
Speed -= 3;
Debug.Log(Speed);
}
else {
// Reset
Animate = false;
Speed = 360;
Chip.transform.localRotation = Quaternion.Euler(0.0,0.0,0.0);
}
}
To summorize this the best I can the gameobject Chip is assigned when it collides on raycast as such
// Set the chip
Chip = hit.transform;
Everything is done in the update function. Once the raycast hits it calls a betting function then after the betting is calculated it changes the Boolean Animate to true causing the spinning of the chip.
Something is setting Animate = true in some other code, hard to tell whats going on without seeing the rest of it.
Put some debug next to every spot where Animate is set to true, you should see something else setting it, only possible explanation as to why it continues to spin.
Another option is to use the Animation tool and instead of rotating, you just play the animation which performs the rotation for you.
Edit: Chances are its around the touch code, cause when you debug in the editor your using key strokes. A gotcha I've experienced a few times.
James Gramosli is correct in that some other code is triggering the animation again and it is most likely your touch code. It is a common problem when moving between editor and a touch-enabled device. You can determine if this is the case by using the UnityRemote to verify the control flow of your code.
That said, I would change your code to the following which removes the spin code from the Update loop that runs every frame. It is a small optimization, but primarily it cleans up the architecture and makes it more modular and a little neater.
It is not clear from your code snippet, but I will assume you are using UnityScript.
In your script that handles the touch code when you click on the chip, insert this line:
hit.transform.SendMessage("Spin", hit.transform, SendMessageOptions.DontRequireReceiver);
Put this code in a separate script called "SpinChip" and then add the script to your chip object.
var StartSpeed = 360.0;
var Deceleration = 3.0;
function Spin()
{
if (Animating)
{
print("Chip is already spinning, not starting another animation");
return;
}
/*
This code isn't necessary if this exists in a separate script and is only ever attached to the clickable chip
if (!gameObject.tag.Contains("Chip"))
{
print("That wasn't a chip you clicked");
return;
}
*/
print("Chip has been told to spin");
StartCoroutine(SpinningAnimation);
}
function SpinningAnimation()
{
print("Chip spin start");
transform.localRotation = Quaternion.identity;
Speed = StartSpeed;
Animating = true;
while (Speed > 0)
{
transform.Rotate(0, Speed*Time.deltaTime, 0);
Speed -= Deceleration;
yield; // wait one frame
}
print("Chip has completed the spin");
Animating = false;
}
What this code does is create a co-routine that runs once per update loop when activated that will spin the chip for you, and is independent of your actual button clicking code.
var rotSpeed: float = 60; // degrees per second
function Update(){
transform.Rotate(0, rotSpeed * Time.deltaTime, 0, Space.World);
}
Here is a code that rotates your game object, you can use it just with a vector 3 :transform.Rotate(x, y, z); or with Space transform.Rotate(x, y, z, Space.World);
rotSpeed is the rotation speed.
In your update function . The Bool variable Animate may becoming true . This may be reason your cylinder continues to rotate.
Other Solution is : You can create an animation of your cylinder and then take a stopwatch . So that after sometime you can stop you animation using the time of stopwatch

Resources