Ver Mensaje Individual
  #13  
Antiguo 05-10-2015
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.211
Reputación: 22
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
He preparado un ejemplo simple con el componente TNMUDP. Lo he probado contra la IP Local 127.0.0.1 en tres copias de la misma aplicación configurando los puertos para que la pelotita salte de una a otra. El Timer está a 5 ms. Publico un pequeño vídeo.

Código PHP:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include <math.h>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Unit2"
#pragma resource "*.dfm"
TForm1 *Form1;

struct TData{
  
char HEADER[3];
  
char SD;
  
double Vx;
  
double Vy;
  
double X;
  
double Y;
};


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : 
TForm(Owner)
{
  
Connect();
  
Timer1->Enabled = false;
  
Frame1->Visible = false;
  
Frame1->DoubleBuffered = true;
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  
double t = 0.01;
  
double Vy = 9.8*t;

  
Y += 1000*(Vy+Voy)*t;
  if(
Y + Frame1->Height >= Panel1->Height){
    
Y = Panel1->Height - Frame1->Height;
    
Voy = -Voy*0.95;   // reducción Y
    
Vox *= 0.98;      // reducción X
  
}else
    
Voy += Vy;

  
X += Vox*t;
  
Frame1->Top = Y;
  
Frame1->Left = X;

  if(
X + Frame1->Width >= Panel1->Width && IsPelota){
    
TData Data;
    
Data.Vx = Vox;
    
Data.Vy = Voy;
    
Data.X = X;
    
Data.Y = Y;
    
NMUDP1->SendBuffer((char*)&Data, sizeof(TData), sizeof(TData));
    
IsPelota = false;
  }
  if(
X >= Panel1->Width){
    
Timer1->Enabled = false;
  }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
  
Connect();
  
IsPelota = true;
  
Vox = 800;     // Velocidad inicial X
  
Voy = 0;       // Velocidad inicial Y
  
Y = 0;
  
X = 0;
  
Frame1->Top = 0;
  
Frame1->Left = 0;
  
Frame1->Visible = true;
  
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
      
int NumberBytes, AnsiString FromIP, int Port)
{
  if(
NumberBytes == sizeof(TData)){
    
TData Data;
    
NMUDP1->ReadBuffer((char*)&Data, sizeof(TData), NumberBytes);
    
Vox = Data.Vx;
    
Voy = Data.Vy;
    
X = 0;
    
Y = Data.Y;
    
Frame1->Left = 0;
    
Frame1->Top = Y;
    
Frame1->Visible = true;
    
Timer1->Enabled = true;
    
IsPelota = true;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Connect()
{
  
NMUDP1->RemoteHost = Edit1->Text;
  
NMUDP1->RemotePort = StrToIntDef(Edit2->Text, 0);
  
NMUDP1->LocalPort  = StrToIntDef(Edit3->Text, 0);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::EditChange(TObject *Sender)
{
  
Connect();
}
//--------------------------------------------------------------------------- 
https://youtu.be/dowjot2OILo

Última edición por escafandra fecha: 05-10-2015 a las 17:38:48.
Responder Con Cita