Author Topic: Moving an object to the center of the screen.  (Read 5120 times)

ciuffolo

  • Guest
Moving an object to the center of the screen.
« on: October 24, 2012, 11:24:30 am »
Hi, i'm going to create an "asteroid remake", but i have another question.
I created the space shuttle and i made the random spawn of the rocks, but i wonder how to move the rocks all in the center of the screen, this is the code that i wrote for the random spawning:
Code: [Select]
TouchAdd(0,0,50,50,rotasinistra,0);
TouchAdd(430,0,50,50,rotadestra,0);
int randomnumber=rand() % 480+1;
int randomnumber2=rand() % 480+1;
int randomnumber3=rand() % 480+1;
int view2;
int view3;
int view4;
view2=ViewAdd("Images/rocks.png",randomnumber,310);
view3=ViewAdd("Images/rocks.png",randomnumber2,310);
view4=ViewAdd("Images/rocks.png",randomnumber3,310);
Thanks

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: Moving an object to the center of the screen.
« Reply #1 on: October 24, 2012, 12:48:44 pm »
Check out this tutorial on Trigonometry for Developers:

http://active.tutsplus.com/tutorials/games/quick-tip-trigonometry-for-flash-game-developers-basix/

It's in Flash, but the concepts are the same.

Cheers,
Tim Sommers
DragonFireSDK Team

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #2 on: October 25, 2012, 12:31:08 pm »
m ... thanks, but i can not do it on c + + Because It Is not really my best language
I know, I'm going to try it but it will very difficult, and if you have a c + + tutorial trigonometry may be better ...
thank you for the support you are giving me

graham

  • Guest
Re: Moving an object to the center of the screen.
« Reply #3 on: October 25, 2012, 02:44:50 pm »
it's not difficult (and you don't need any trigonometry ;) )

to move an object in a straight line, just compute coordinates as a linear combination of the start and end points:

Code: [Select]
float x1, y1;  // starting position
float x2, y2;  // end position
float x, y;      // the point that moves
float alpha;

for (alpha=0.0; alpha<=1.0; alpha+=0.1) {
   x = alpha*x1+(1.0-alpha)*x2;
   y = alpha*y1+(1.0-alpha)*y2;
}


this will move (x,y) from (x1,y1) to (x2,y2) in a straight line using ten equidistant steps.

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #4 on: October 26, 2012, 07:28:40 am »
Thank you, i'm going to try it, but i have to study trigonometry too because it can help me in the future.

and how to apply it to an image?
do i have to put in x1 and y1 the location of the image?
m...i don't think...
« Last Edit: October 26, 2012, 12:17:51 pm by ciuffolo »

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #5 on: December 17, 2012, 12:53:24 pm »
thank you, did it, but still have some problems now.How to use it in a for cicle?
i did it:
Code: [Select]
for (int i = 0; i < 20; i++)
    {
if(alpha<=1.0)
{
ENX=ViewGetx(EN[i]);
ENY=ViewGety(EN[i]);

       
alpha+=0.01; // aumentando questo valore aumenti la velocità
x = alpha*240+(1.0-alpha)*ENX;
y = alpha*160+(1.0-alpha)*ENY;
ViewSetxy(EN[i],x,y);

}
    }
but doesn't work...

graham

  • Guest
Re: Moving an object to the center of the screen.
« Reply #6 on: December 18, 2012, 01:40:46 am »
i dont know what you mean by "How to use it in a for cicle?"

there is a bug in your code: you should increment alpha outside the i loop.

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #7 on: December 18, 2012, 07:56:35 am »
i was wrong, i wanted to mean in  a "for loop".
I'm going to try if that was the problem

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #8 on: December 18, 2012, 10:26:04 am »
In short I have to create a ball and start it, and after a second I need to create another one and get it started, but I also make sure that they travel at the same time.
i only want to know how to do it, not the code.

graham

  • Guest
Re: Moving an object to the center of the screen.
« Reply #9 on: December 19, 2012, 02:35:04 pm »
i can't think how to describe it concisely without using code, so here is a suggestion:

Code: [Select]
#include "DragonFireSDK.h"
#include <string.h>
#include <math.h>
#include <time.h>


const int CenterX = 160;
const int CenterY = 240;
const int INVISIBLE = 0;
const int VISIBLE = 1;


int AsteroidX[10];
int AsteroidY[10];
float Alpha[10];
int Counter;
int AsteroidI;
int AsteroidV[10];
int Tick;
int AsteroidStartX[10];
int AsteroidStartY[10];
float Delta = 0.002;


void Initialize()
{
int i;
float Angle;
float Distance;

RandomSetSeed(TimerGetMs());
AsteroidI = ImageAdd("Images/Asteroid.png");
for (i=0; i<10; i++) {
Angle = 2.0*3.1416*0.01*Random(100);
Distance = Random(30)+130;
AsteroidStartX[i] = 160+Distance*cos(Angle);
AsteroidStartY[i] = 240+Distance*sin(Angle);
AsteroidV[i] = ViewAdd(AsteroidI, AsteroidStartX[i], AsteroidStartY[i]);
ViewSetVisible(AsteroidV[i], INVISIBLE);
Alpha[i] = 0.0;
}
ViewSetVisible(AsteroidV[0], VISIBLE);
Counter = 1;
Tick = 0;
}


void Step()
{
int i;
int x, y;

for (i=0; i<Counter; i++)
if (Alpha[i] < 1.0) {
Alpha[i] += Delta;
x = (1.0 - Alpha[i]) * AsteroidStartX[i] + Alpha[i] * (CenterX-15);
y = (1.0 - Alpha[i]) * AsteroidStartY[i] + Alpha[i] * (CenterY-15);
ViewSetxy(AsteroidV[i], x, y);
}
}


void AppMain()
{
Initialize();
}


void AppExit()
{
}


void OnTimer()
{
int i;

Tick++;
Step();
if ((Tick % 30) == 0)
if (Counter < 10) {
ViewSetVisible(AsteroidV[Counter], VISIBLE);
Counter++;
}
}

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #10 on: December 20, 2012, 03:12:10 pm »
thanks, I'm going to try it.
There is only one thing that leaves me perplexed, the part of the sine and cosine ... but it doesn't seems something excessively complicated, thanks again

graham

  • Guest
Re: Moving an object to the center of the screen.
« Reply #11 on: December 21, 2012, 02:59:09 am »
that's just one possible way to determine starting positions for the asteroids: choose an angle and a distance from the centre of the screen at random and then convert those two numbers to (x,y) coordinates. it can easily be done without using trigonometric functions.

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #12 on: December 21, 2012, 09:51:10 am »
oh yes, but i alredy have the random starting position, i only want to know how to move two asteroids togheter, but you helped me a alot, thanks!
i didn't think to making alpha an array.
I am a self-taught, that's why i'm not so good with these things...i'm studying it at school. :)

ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #13 on: December 27, 2012, 02:48:20 pm »
for now I used the random function and my orthodox methods that i will semplify later, but in doing that they do not move anymore, they only create ... and I left the rest unchanged
the method that you use to create the rocks in this part of the code has to do with moving them or can I also use a random function? Maybe I've made some mistakes ...


ciuffolo

  • Guest
Re: Moving an object to the center of the screen.
« Reply #14 on: December 27, 2012, 03:25:17 pm »
sorry,sorry i solved:
i have made
INT delta=0.002
then it takes only the 0...
i must make
FLOAT delta=0.002