Touch screen scrolling

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

Touch screen scrolling

Post by Imerion »

I have been trying to add touch screen controls to my latest game, Resource Wars. (http://gamejolt.com/games/resource-wars/80954)

I was thinking that using two fingers to drag the playing field around would be practical, but I'm not sure I'm doing any of this right.
Currently, my very simple implementation looks like this :

Code: Select all


if (TouchDrag >= 0.1 && OldTouchX < App.CameraPosition.X + TouchGetX(0) * 8) {
  if (App.CameraPosition.X > 3.6)
    App.CameraPosition.X -= 5 * App.DeltaTime;
}

if (TouchDrag >= 0.1 && OldTouchX > App.CameraPosition.X + TouchGetX(0) * 8) {
  if (App.CameraPosition.X < 11.4)
    App.CameraPosition.X += 5 * App.DeltaTime;
}

if (TouchDrag >= 0.1 && OldTouchY > App.CameraPosition.Y + TouchGetY(0) * 4.5) {
  if (App.CameraPosition.Y < 14.9)
    App.CameraPosition.Y += 5 * App.DeltaTime;
}

if (TouchDrag >= 0.1 && OldTouchY < App.CameraPosition.Y + TouchGetY(0) * 4.5) {
  if (App.CameraPosition.Y > 4.1)
    App.CameraPosition.Y -= 5 * App.DeltaTime;
}

if (touchGetCount()>=2) {
  OldTouchX = App.CameraPosition.X + TouchGetX(0) * 8;
  OldTouchY = App.CameraPosition.Y + TouchGetY(0) * 4.5;
  if (TouchDrag < 0.1)
    TouchDrag += 1 * App.DeltaTime;
}


if (touchGetCount()<=0)
  TouchDrag = 0;
Any idea how I should go about this?
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

Re: Touch screen scrolling

Post by Imerion »

I think I got this working. Was an oversight on my part. Code now looks like this :

Code: Select all

if (TouchDrag >= 0.1 && OldTouchX < App.CameraPosition.X + TouchGetX(0) * 8) {
  if (App.CameraPosition.X > 3.6)
    App.CameraPosition.X -= 6 * App.DeltaTime;
}

if (TouchDrag >= 0.1 && OldTouchX > App.CameraPosition.X + TouchGetX(0) * 8) {
  if (App.CameraPosition.X < 11.4)
    App.CameraPosition.X += 6 * App.DeltaTime;
}

if (TouchDrag >= 0.1 && OldTouchY > App.CameraPosition.Y + TouchGetY(0) * 4.5) {
  if (App.CameraPosition.Y < 14.9)
    App.CameraPosition.Y += 6 * App.DeltaTime;
}

if (TouchDrag >= 0.1 && OldTouchY < App.CameraPosition.Y + TouchGetY(0) * 4.5) {
  if (App.CameraPosition.Y > 4.1)
    App.CameraPosition.Y -= 6 * App.DeltaTime;
}

if (touchGetCount()>=1) {
  OldTouchX = App.CameraPosition.X + TouchGetX(0) * 8;
  OldTouchY = App.CameraPosition.Y + TouchGetY(0) * 4.5;
  if (TouchDrag < 0.1)
    TouchDrag += 1 * App.DeltaTime;
}


if (touchGetCount()<1)
  TouchDrag = 0;
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Touch screen scrolling

Post by Kjell »

Hi Imerion,

You shouldn't be using Delta Time for this. Touch coordinates aren't influenced by the frame-rate ( obviously ).

Below is a simple mouse-based example ( hold left-click to scroll ) .. but the concept is exactly the same when using touch.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnUpdate>
    <ZExpression>
      <Expression>
<![CDATA[// Store previous & current mouse coordinates

Mouse[1] = Mouse[0];
Mouse[0] = App.MousePosition;

// Reset mouse button to FALSE

Button = 0;]]>
      </Expression>
    </ZExpression>
    <KeyPress Keys="{">
      <OnPressed>
        <ZExpression>
          <Expression>
<![CDATA[// Set mouse button to TRUE

Button = 1;]]>
          </Expression>
        </ZExpression>
      </OnPressed>
    </KeyPress>
    <Condition Expression="return Button;">
      <OnTrue>
        <ZExpression>
          <Expression>
<![CDATA[// Calculate the movement scalar based on the FOV ..
// .. if you're using a Orthographic camera, you don't need to calculate this.

float s = App.CameraPosition.Z*tan(App.FOV*PI/360);

// Calculate the screen aspect ratio

float w = App.ScreenWidth*1f/App.ScreenHeight;

// Camera movement

App.CameraPosition.X -= (Mouse[0].x-Mouse[1].x)*s*w;
App.CameraPosition.Y -= (Mouse[0].y-Mouse[1].y)*s;]]>
          </Expression>
        </ZExpression>
      </OnTrue>
    </Condition>
  </OnUpdate>
  <OnRender>
    <UseMaterial Material="LineMaterial"/>
    <RenderTransformGroup Scale="8 8 1">
      <Children>
        <RenderNet XCount="7" YCount="7" RenderVertexExpression="//"/>
      </Children>
    </RenderTransformGroup>
  </OnRender>
  <Content>
    <Array Name="Mouse" Type="6" SizeDim1="2"/>
    <Variable Name="Button" Type="4"/>
    <Material Name="LineMaterial" WireframeWidth="1" Shading="2" Light="0" ZBuffer="0"/>
  </Content>
</ZApplication>
K
Imerion
Posts: 200
Joined: Sun Feb 09, 2014 4:42 pm

Re: Touch screen scrolling

Post by Imerion »

That is logical now that I think about it. Have just gotten so used to using DeltaTime I do it everywhere. :)
I had quite a few problems with the camera jumping around, since coordinates would be off when fingers were lifted. Hard to explain, but I had to make sure no new movements were recorded after both fingers were lifted. The code I ended up with, and which now works fine, looks like this. :)

Code: Select all

if (touchGetCount() >= 1 && OldTouchX != 0 && OldTouchX < TouchGetX(0) * 8) {
    App.CameraPosition.X -= ((TouchGetX(0) * 8) - OldTouchX) * 0.6;
}

if (touchGetCount() >= 1 && OldTouchX != 0 && OldTouchX > TouchGetX(0) * 4.5) {
    App.CameraPosition.X -= ((TouchGetX(0) * 8) - OldTouchX) * 0.6;
}

if (touchGetCount() >= 1 && OldTouchY != 0 && OldTouchY > TouchGetY(0) * 8) {
    App.CameraPosition.Y -= ((TouchGetY(0) * 4.5) - OldTouchY) * 0.6;
}

if (touchGetCount() >= 1 && OldTouchY != 0 && OldTouchY < TouchGetY(0) * 4.5) {
    App.CameraPosition.Y -= ((TouchGetY(0) * 4.5) - OldTouchY) * 0.6;
}


if (App.CameraPosition.X < 3.6)
  App.CameraPosition.X = 3.6;

if (App.CameraPosition.X > 11.4)
  App.CameraPosition.X = 11.4;

if (App.CameraPosition.Y > 14.9)
  App.CameraPosition.Y = 14.9;

if (App.CameraPosition.Y < 4.1)
  App.CameraPosition.Y = 4.1;

OldTouchX = 0;
OldTouchY = 0;

if (touchGetCount() >= 1) {
  OldTouchX = TouchGetX(0) * 8;
  OldTouchY = TouchGetY(0) * 4.5;
}
Post Reply