XNA Hold screen to move object - windows-phone-7

im working with windows phone game using xna... and looking a way to make my object to move with holding some rectangle that i made as a button on screen....
i already tried this but it just read tap gesture only
foreach (GestureSample gestureSample in input.Gestures)
{
switch (gestureSample.GestureType)
{
case GestureType.Hold:
case GestureType.Tap:
Point tapLocation = new Point((int)gestureSample.Position.X, (int)gestureSample.Position.Y);
if (rightRectangle.Contains(tapLocation))
{
rightTouched = true;
player.Catapult.CurrentState = CatapultState.MoveRight;
playerPositionUpdate.X += 20;
player.catapultPosition.X += 20;
player.Catapult.catapultPosition.X += 20;
player.Catapult.projectilePositionUpdate.X += 20;
if (player.Catapult.catapultPosition.X == player.Catapult.catapultPosition.X + 20)
rightTouched = false;
CenterOnPosition(player.Catapult.Position - catapultCenterOffset);
}
break;
}
}

Gestures are discrete events. You want to poll the state of the touch panel every frame. Use TouchPanel.GetState() for that.
See Working with Touch Input (Windows Phone) for information.

As mentioned above you shouldn't use GestureSample for that purpose, because using dragcomplete and freedrag a bit tricky. Instead you should code your own gestures using TouchPanel.GetState()
TouchCollection touchCollection = TouchPanel.GetState();
if (touchCollection.Count == 1)
{
var touch = touchCollection[0];
switch (touch.State)
{
case TouchLocationState.Pressed:
if (rectangle.Contains((int) touch.Position.X, (int) touch.Position.Y))
{
isMoving = true;
}
break;
case TouchLocationState.Moved:
if (isMoving)
{
TouchLocation prevLocation;
touch.TryGetPreviousLocation(out prevLocation);
if (prevLocation.Position != touch.Position)
{
Vector2 delta = touch.Position - prevLocation.Position;
//offset your rectangle on delta
}
}
break;
case TouchLocationState.Released:
isMoving = false;
break;
}
}
else if (touchCollection.Count == 2)
{
var touchOne = touchCollection[0];
var touchTwo = touchCollection[1];
//pinch logic here
}
I guess you understand the idea.

Related

Memory leak in SDL2

I'm trying to solve this one but I am stumped. When I continuously hover over an object, the ram usage increases. The hover code is below:
//Small memory leak here. Need to figure this out.
void PlayerInteraction::InteractionControllerHover(std::string interactionMessage) {
SDL_DestroyTexture(Scene1::ftexture);
SDL_FreeSurface(Scene1::fsurface);
const char* im = interactionMessage.c_str();
if (interactionMessage != "" ) {
int interactionMessagelength = interactionMessage.length();
Scene1::textRect = { 500, 610, interactionMessagelength * 10, 20 };
Scene1::fsurface = TTF_RenderText_Solid(Scene1::font, im, Scene1::fcolor);
Scene1::ftexture = SDL_CreateTextureFromSurface(Scene1::renderer, Scene1::fsurface);
//_sleep(70) seems to slow the leak down.
}
}
if I add _sleep(70) to the code, the memory leak is much slower, however I can't figure out why its leaking because I am destroying the texture each time the hover occurs. I don't like adding sleep because it causes other issues. Any advice?
//Mouse Hover Game Interaction.
case SDL_MOUSEMOTION:
//Event Motion coordinates. Where the mouse moves on the screen.
x = event.motion.x;
y = event.motion.y;
gd = gdSprite.x;
gy = gdSprite.y;
//This addresses the movement to the left issue where the player never reaches to destination and prevents hover interaction.
if (playerMessage != true && interactionMessage == "") {
//Prevents sleep from kicking in when walking to a target.
if (gdSprite.x < gd && gdSprite.y < y || gdSprite.x > gd && gdSprite.y > y) {
ftexture = SDL_CreateTextureFromSurface(renderer, fsurface); //Potentially fixes sprite appearing in text.
SDL_DestroyTexture(ftexture);
playerIsMoving = 0;
}
else {
ftexture = SDL_CreateTextureFromSurface(renderer,fsurface); //Potentially fixes sprite appearing in text.
SDL_DestroyTexture(ftexture);
SDL_DestroyTexture(Textures::spriteTexture);
Textures::spriteTexture = SDL_CreateTextureFromSurface(renderer, Textures::spriteDown1); //Makes player face you when you are hovering.
}
interactionMessage = pob.HoverObjects(x, y, scene, gd, gy);
}
if (interactionMessage != "" && playerIsMoving !=1) {
_sleep(70); //reduces memory leak when repeatedly hovering over objects in quick succession.
pi.InteractionControllerHover(interactionMessage);
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
}
break;
}
}
Thanks for the advice. This seems to have fixed the problem:
void PlayerInteraction::InteractionControllerHover(std::string interactionMessage) {
const char* im = interactionMessage.c_str();
if (interactionMessage != "" ) {
int interactionMessagelength = interactionMessage.length();
Scene1::textRect = { 500, 610, interactionMessagelength * 10, 20 }; // The * 10, 20 is a mathematical way of setting the text width depending on the length of the text.
Scene1::fsurface = TTF_RenderText_Solid(Scene1::font, im, Scene1::fcolor);
Scene1::ftexture = SDL_CreateTextureFromSurface(Scene1::renderer, Scene1::fsurface);
SDL_FreeSurface(Scene1::fsurface);
}
//Mouse Hover Game Interaction.
case SDL_MOUSEMOTION:
//Event Motion coordinates. Where the mouse moves on the screen.
x = event.motion.x;
y = event.motion.y;
gd = gdSprite.x;
gy = gdSprite.y;
//This addresses the movement to the left issue where the player never reaches to destination and prevents hover interaction.
if (playerMessage != true && interactionMessage == "") {
//Prevents sleep from kicking in when walking to a target.
if (gdSprite.x < gd && gdSprite.y < y || gdSprite.x > gd && gdSprite.y > y) {
SDL_DestroyTexture(ftexture);
playerIsMoving = 0;
}
else {
SDL_DestroyTexture(ftexture);
SDL_DestroyTexture(Textures::spriteTexture);
Textures::spriteTexture = SDL_CreateTextureFromSurface(renderer, Textures::spriteDown1); //Makes player face you when you are hovering.
}
interactionMessage = pob.HoverObjects(x, y, scene, gd, gy);
}
if (interactionMessage != "" && playerIsMoving !=1) {
pi.InteractionControllerHover(interactionMessage);
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
}
break;
}
}

Optimizing Ragdoll Behaviour in Unity

I'm trying to make a realistic transition between a walking animation and the falling process with a ragdoll. Basically when calling the "DoRagdoll" function, I'm adding the previous force from the walking animation to all rigidbody components in respect of their masses. Then I deactivate the animator component and turn on gravity for all rigidbodies. It works but it still kinda looks weird. I already tried a lot like the Unity documentation site for problems with ragdolls and I also played around with the joints of the different bodyparts. I used the buildin system to create the ragdoll. Any Ideas on how to improve the transition or how to create a more realistic ragdoll?
void Awake()
{
rbs = GetComponentsInChildren<Rigidbody>();
DoRagdoll(false);
foreach (Rigidbody rb in rbs)
{
rb.useGravity = false;
//rb.isKinematic = true;
}
}
public void DoRagdoll (bool isRagdoll)
{
if (isRagdoll)
{
foreach (Rigidbody rb in rbs)
{
rb.AddForce(rb.mass * rb.velocity, ForceMode.Impulse);
}
}
GetComponent<Animator>().enabled = !isRagdoll;
if (isRagdoll)
{
foreach (Rigidbody rb in rbs)
{
rb.maxDepenetrationVelocity = 0.01f;
rb.useGravity = true;
//rb.isKinematic = false;
}
NavMeshAgent agent = GetComponent<NavMeshAgent>();
//GameObject.FindGameObjectWithTag("pelvis").GetComponent<PelvisPush>().PushPelvis();
rbSlowdown = true;
ragdollOn = true;
}
else
{
GetComponent<NavMeshAgent>().speed = GameObject.FindGameObjectWithTag("root").GetComponent<OldmanMovement>().movSpeed;
foreach (Rigidbody rb in rbs)
{
rb.useGravity = false;
//rb.isKinematic = true;
}
ragdollOn = false;
}
}

In UnityScript, why does GUI not get called within hit on collider?

I'm trying to print out text when a user touches a sprite, however, even though I get no errors when building and running the code, the code refuses to printout the text and I cannot understand what I am doing wrong. Please help me understand why it does not print.
It prints the debug text when I touch the sprite, but not the GUI text with total.
This is the code:
#pragma strict
function Start () {
OnGUI();
}
function OnGUI(){
//var total = 0;
//GUI.Label( myRect, total.ToString() ); // Displays "10".
//GUI.Label( myRect, "" + bullets ); // Displays "10".
// GUI.Label(Rect(0,0,Screen.width,Screen.height),"Total:"+total);
}
//function Update () {
//}
var platform : RuntimePlatform = Application.platform;
function Update(){
if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
if(Input.touchCount > 0) {
if(Input.GetTouch(0).phase == TouchPhase.Began){
checkTouch(Input.GetTouch(0).position);
}
}
}else if(platform == RuntimePlatform.WindowsEditor){
if(Input.GetMouseButtonDown(0)) {
checkTouch(Input.mousePosition);
}
}
}
function checkTouch(pos){
var total = 0;
var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
var touchPos : Vector2 = new Vector2(wp.x, wp.y);
var hit = Physics2D.OverlapPoint(touchPos);
if(hit){
GUI.Label(Rect(0,0,Screen.width,Screen.height),"Total:");
Debug.Log(hit.transform.gameObject.name);
hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
//total = total +1;
}
}
You cannot put a gui item outside the OnGUI Method. You need to set a boolean to turn on the label.
Something along these lines.
OnGUI(){
if(labelbool)
GUI.Label ....
}
check(){
labelbool = true;
}

Unity3D - GUIText Does Not Show On Collision With Object

Ive been following this article online and have swapped a few names and tags about but I dont seem to see any text appearing on the screen...
Here is my PlayerCollision script:
#pragma strict
function Update () {
}
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "Collider"){
ShowMessage.message = "HELLO WORLD";
ShowMessage.turnTextOn = true;
}
}
This is my ShowMessage script:
#pragma strict
static var turnTextOn : boolean = false;
static var message : String;
private var timer : float = 0.0;
function Start(){
timer = 0.0;
turnTextOn = false;
guiText.text = "";
}
function Update () {
if(turnTextOn){
guiText.enabled = true;
guiText.text = message;
timer += Time.deltaTime;
}
if(timer >= 5){
turnTextOn = false;
guiText.enabled = false;
timer = 0.0;
}
}
I have linked the ShowMessage script to my GUIText object and have linked the PlayerCollision script with the CharacterController. There is also a box collider object with the Collision tag I also Have the GUIText in view just to rule that out.
Anyone any idea what is wrong? Thanks
The tag should be "Collider" not "Collision" because if(hit.gameObject.tag == "Collider")
Problem solved. Turns out I ticked isTrigger which stopped the collision from being detected. This gives the problem of the character not being able to move through the object. What i did was tick the isTrigger option and change OnControllerColliderHit(hit : ControllerColliderHit) to OnTriggerEnter (obj : Collider)

Farseer/XNA Assertion Failed, Vector2 position for body modified by camera matrix

I created a camera with a matrix and used it to move the view point in 2D. Basically I started from this template:
http://torshall.se/?p=272
I also had in one of my class, a simple code to spawn boxs with the mouse:
public void CreateBodies()
{
mouse = Mouse.GetState();
if (mouse.RightButton == ButtonState.Pressed)
{
Bodies += 1;
if (Bodies >= MaxBodies)
Bodies = 0;
rectBody[Bodies] = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(rectangle.Width), ConvertUnits.ToSimUnits(rectangle.Height), 1);
rectBody[Bodies].Position = ConvertUnits.ToSimUnits(mouse.X, mouse.Y);
rectBody[Bodies].BodyType = BodyType.Dynamic;
}
}
This Worked perfectly fine but when I moved the ''camera'' the mouse didn't change in the right location, Si I did this little modification in game1.cs and in my method to have the world coord. of my mouse:
mouse = Mouse.GetState();
Matrix inverse = Matrix.Invert(camera.transform);
Vector2 mousePos = Vector2.Transform(new Vector2(mouse.X, mouse.Y), inverse);
TE.CreateBodies(mousePos);
public void CreateBodies(Vector2 mousePosition)
{
mouse = Mouse.GetState();
MousePosition = mousePosition;
if (mouse.RightButton == ButtonState.Pressed)
{
Bodies += 1;
if (Bodies >= MaxBodies)
{
Bodies = 0;
}
rectBody[Bodies] = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(rectangle.Width), ConvertUnits.ToSimUnits(rectangle.Height), 1);
rectBody[Bodies].BodyType = BodyType.Dynamic;
rectBody[Bodies].Position = ConvertUnits.ToSimUnits(MousePosition);
}
}
Now this is supposed to give me the world coords. of my mouse, but I have a problem, when I run the program and click somewhere on the screen to create a box I get this error:
http://img68.xooimage.com/files/6/a/4/bob-2c526f4.png
What's going on? :/
Edit:
This is at the line 439 of body.cs:
Debug.Assert(!float.IsNaN(value.X) && !float.IsNaN(value.Y));

Resources