How would I go about making a sprite rotate depending on the location of the mouse? Could someone give me a general idea on how to do it.(I'm using SFML and C++)
What type of rotation are you talking about? Do you want the angle to change as you move the mouse in along the X axis or do you want to have the sprite point towards the cursor?
I want the sprite to point towards the cursor.
Use Atan2. In pseudocde:If that doesnt work, try flipping the arguments as in mouse.X - object.X and mouse.Y - object.Y. The returned angle will be in radians.Code:rotation = Atan2(mouse.Y - object.Y, mouse.X - object.Y)
Additionally, here's something I recently learned relating to atan2:
can also be done with 2d vectors by using the perpendicular dot product:Code:angleBetween = atan2(cross(v1, v2), dot(v1, v2))
Edited:Code:angleBetween = atan2(perpDot(v1, v2), dot(v1, v2))
where v1 and v2 are 3d vectors in the first code example, and 2d vectors in the second.
Isn't that the same as acos(dot(v1, v2))?
vvvvvv Completely forgot about that.
Yes, but atan2 is prefered over acos because of inaccuracies when nearing 0 and pi. Also atan2 gives the direction of rotation from v1 to v2 where acos does not, because the range of atan2 is [-pi, pi) where the range of acos is (0, pi)
I'm getting strange compiler errors with VC++ :'(
http://www.facepunch.com/threads/1152160
Resolved thanks guys! :D
I did this but it still doesn't work properly:
MouseRad = atan2(sf::Mouse::GetPosition().x - shipSpr.GetPosition().x, sf::Mouse::GetPosition().y - shipSpr.GetPosition().y); MouseDeg = (MouseRad) * (180/PI); shipSpr.SetOrigin(shipSpr.GetPosition().x/2, shipSpr.GetPosition().y/2); shipSpr.Rotate(MouseDeg);
Quoting from last thread because Garry closed it on me, the bellend.
Nigey Nige posted:Shadow casting sounds interesting, but I want the map to slowly turn from grey fog into explored regions and stay that way once uncovered. Is that the best way of doing it?reevezy67 posted:
I need help with VC++ 2010 express. I need to set up my additional include directories from an environment variable so my entire team can use the same directories. How would I do this?
There's an issue with what I assume is either my installation of XNA, or visual studio. Whenever I save a project, the content folder for said project instantly thinks it's corrupt, even though it can still access the content within it. This prevents me from adding any new resources, and is very frustrating. What can I do to fix this?
The content folder is doing this:
Help would really be appreciated, I've been locked from programming for almost two weeks because of this.
.Rotate() does it relative to current rotation if you want to set rotation you should use .SetRotation() instead.
Reinstall XNA/Visual Studio?
Or see if you can edit the content-project manually. It won't be fun but it might work.
You got the X and Y backwards in atan2, it seems weird but it's Y then X.
Try this:
Code:MouseRad = atan2(sf::Mouse::GetPosition().y - shipSpr.GetPosition().y, sf::Mouse::GetPosition().x - shipSpr.GetPosition().x);
Edit it how so? As in edit the actual folder's contents?
Post #19
Okay, I changed my code to this but it seems that the sprite is not rotating on its own center even though I made the sprite's origin in the center of the sprite:
MouseRad = atan2(sf::Mouse::GetPosition().y - shipSpr.GetPosition().y, sf::Mouse::GetPosition().x - shipSpr.GetPosition().x); MouseDeg = (MouseRad) * (180/PI); shipSpr.SetOrigin(shipSpr.GetPosition().x/2, shipSpr.GetPosition().y/2); shipSpr.SetRotation(MouseDeg);
I think I found the problem, I'm setting the origin to the position where the sprite is, not on the sprite.
The Reference Content is set up as it's own visual-studio project inside it's folder correct? See if you can muck around with just that (and not your big project that includes it).
How come math.random() in LOVE isn't random.
Because you're not seeding it, presumably.
try putting math.randomseed(os.time()) in love.load(). Only call it once.
Because the seed doesn't change(but does when you call random). Just put math.random() in the update function to get better random numbers.
Edited:
And that
SetOrigin() is relative to the top left corner of the object, so instead you should use something like that :
shipSpr.SetOrigin(shipSpr.GetTexture().GetWidth()/2, shipSpr.GetTexture().GetHeight()/2);
I tried this and it worked, it put the origin on the sprite. Now just to fix the rotation.
That's not how a psuedo-RNG works.
For the same seed, the order of 'random' numbers will be the same each time. Each seed value gives a different order of random numbers. This is how any procedural game with map seeds work - that seed is given to the RNG, which spits out the same 'random' numbers in the same order on any machine.
You are just describing calling the RNG constantly - both a waste of cpu cycles and unfit for purpose - if the program runs at peak speed every time your numbers would still not be random each execution. Seeding to the current system clock guarantees that the numbers will be a different sequence each time.
Except they still aren't even after I seed it in love.load().
well i stuck for a while, i wanna make a program with 5 column of numbers and wanna increase them whenever the user input's a number from 1 to 5 for example if the user enters 2 it will increase all of the numbers by 2. but im stuck at returning from queryFunc to main function. and without it program cant keep working. if i can do this i will add the else statement and finish my job. hope you guys can help me im a little bit beginner in C++
#include <iostream> using namespace std; void drawFunc(); void askFunc(); void queryFunc(); int main() { int x = 1; drawFunc(); askFunc(); queryFunc(); return 0; } void drawFunc() { int x = 1; while(x <= 5) { cout << x << "|" << x << "|" << x << "|" << x << "|" << x << endl; x = x++; } } void askFunc() { int x = 1; cout << "Enter a number from 1 to 5: "; cin >> x; } void queryFunc() { int x = 1; if(x == 2 | 3 | 4 | 5) { /* i wanna return to the main function from here but i cant as i said above. */ } }
also defining same variable in all functions is weird i dont know how i can shorten it also is there a way to make the columns dynamic without need for go down ?
What OS?
Does anyone know how I'd make a certain date show up in a timer for Android?
That is what I read to do, but I have no idea where to put this.Code:Calendar c = Calendar.getInstance(); int seconds = c.get(Calendar.SECOND)
Would I put it under strings.xml or layout.xml? I'm confused as hell.
This is what I have currently:
Code:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="30dp"> <TextView android:id="@+id/day" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Day " android:textSize="36sp" /> <TextView android:id="@+id/hour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00" android:textSize="36sp" /> <TextView android:id="@+id/min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":00" android:textSize="36sp" /> <TextView android:id="@+id/sec" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=":00" android:textSize="36sp" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter End Time : " /> <EditText android:id="@+id/end" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Start Time : " /> <EditText android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="40dp" > <Button android:id="@+id/btnstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Counter" /> <Button android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" /> </LinearLayout> </LinearLayout>
I think this will work:
#include <iostream> using namespace std; void drawFunc(int x); void askFunc(); int main() { int x = 1; while(true) { drawFunc(x); x += askFunc(); //adds to the current value of x } return 0; } void drawFunc(int x) //print five rows of five columns, each row with five columns of x plus 1 to 5. { int i = 1; while(i <= 5) { cout << x + i << "|" << x + i << "|" << x + i << "|" << x + i << "|" << x + i << endl; i++ //increment } } void askFunc() { int x = 0; cout << "Enter a number from 1 to 5: "; cin >> x; if((x == 1 || x == 2 || x == 3 || x == 4 || x == 5) == false) //not 1-5? { cout << "Not a valid number."; x = 0; //don't add anything } return x; }
Edited:
The queryFunc wasn't really necessary, and you were initializing x as 1 locally in each function.
In function 'int main()':|
error: void value not ignored as it ought to be|
in evaluation of 'operator+=(int, void)'|
In function 'void drawFunc(int)':|
|28|error: expected ';' before '}' token|
In function 'void askFunc()':|
|41|error: return-statement with a value, in function returning 'void'|
||=== Build finished: 4 errors, 0 warnings ===|
thanks for your interest but it gives bunch of errors ?
Is this any better?
#include <iostream> using namespace std; void drawFunc(int x); int askFunc(); //was void int main() { int x = 1; while(true) { drawFunc(x); x += askFunc(); //adds to the current value of x } return 0; } void drawFunc(int x) //print five rows of five columns, each row with five columns of x plus 1 to 5. { int i = 1; while(i <= 5) { cout << x + i << "|" << x + i << "|" << x + i << "|" << x + i << "|" << x + i << endl; i++; //increment //forgot the semicolon } } int askFunc() //was void { int x = 0; cout << "Enter a number from 1 to 5: "; cin >> x; if((x == 1 || x == 2 || x == 3 || x == 4 || x == 5) == false) //not 1-5? { cout << "Not a valid number."; x = 0; //don't add anything } return x; }
Although you should compare both to learn how to identify those types of errors
I'm trying to use Awesonium to render to a ID3D11Texture2D (or associated ID3D11ShaderResourceView).
I've gone through the steps of setting up Awesonium and rendering, and then I use this code:
//Copy to buffer const awe_renderbuffer *renderBuffer = awe_webview_render(webView); if(renderBuffer) { //Copy buffer to texture unsigned char *myBuffer; myBuffer = new unsigned char[512*512*4]; awe_renderbuffer_copy_to(renderBuffer, myBuffer, 512 * 4, 4, true, false); Renderer()->BindDynamicTexture2D(myBuffer); delete[] myBuffer; }
(Unfortunately I have to use the C API for Awesonium due to using Visual Studio 2010).
Where Renderer()->BindDynamicTexture2D is:
void RenderSystem::BindDynamicTexture2D(unsigned char *buffer) { //Map it D3D11_MAPPED_SUBRESOURCE ms; GetRenderBackend()->m_pDeviceContext->Map(m_pTexture2D, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); memcpy(ms.pData, buffer, sizeof(buffer)); GetRenderBackend()->m_pDeviceContext->Unmap(m_pTexture2D, NULL); //Re-bind it HRESULT result = GetRenderBackend()->m_pDevice->CreateShaderResourceView(m_pTexture2D, NULL, &m_pDynamic2DView); }
Where m_pTexture2D is made by:
//Create our 'dynamic' texture D3D11_TEXTURE2D_DESC desc; ZeroMemory(&desc, sizeof(desc)); desc.Width = 512; desc.Height = 512; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; desc.Usage = D3D11_USAGE_DYNAMIC; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; desc.SampleDesc.Count = 1; GetRenderBackend()->m_pDevice->CreateTexture2D(&desc, NULL, &m_pTexture2D);
I then try to bind the m_pDynamic2DView to the shader and draw it on a quad. Unfortunately it just displays black. If I bind a normal texture loaded from file, it shows up fine.
"HRESULT result = GetRenderBackend()->m_pDevice->CreateShaderResourceView(m_pTexture2D, NULL, &m_pDynamic2DView);" returns S_OK; so I think it copied the data okay, but it's obviously wrong.
Anyone have any experience with either Awesonium or dynamic-usage ID3D11Texture2D's?
I'm working on FPS camera control, what would be the simplest way to turn the mouse difference from the center (X and Y values) into the proper rotations?
sizeof(buffer) is the size of the pointer. Send the actual size as another parameter. Also you shouldn't need to create another view.
thank you very much
Windows. I'll do some testing, brb
-snop-