Thanks for the examples. The first one has smooth transitions in motion. Exactly what I need. However I'm using .net(managed code) not winAPI. It seems .net rounds the floats down to a whole number? Would you be able to help me with the formula for moving an object using my previous post idea? Thanks.
Cyclone_S
Posts
-
Moving Objects in small increments -
Moving Objects in small incrementsI'm not sure I understand. Do you understand what I'm trying to do? after the ball hits the paddle it should move left/right depending on how far from the center it is. Just need finer precision on the angle(xvel) of the ball. Thanks. I tried something like this. I took a number like 1.65 and did
1.65 - Math::Floor(1.65)
to get just the decimal part of the number 0.65.Then every frame I added 0.65 to 0.65 and checked if it was >= 1. If it was I added 1.0 to the left/right property of the box. I then repeated that. The problem with it was I wasn't encounting for the speed of the ball. If the speed of the ball is 6 it moves 6 frames every timer tick. The code I was trying only moved 1 pixel at a time. I'm just not sure on the math to incorporate the speed into it.
-
Moving Objects in small incrementsI tried using doubles but for a value like 0.6 the ball doesn't move at all. The box control takes doubles but will round them down to the nearest integer. Is there a way to do this over the course of X number of frames? I'm using non-integers for the box control's position. Right now there's only about 6 different directions the ball takes when hitting the paddle. I need finer control over the speed of the ball. This problem happens with any sort of animation I try to do. What if I wanted smooth acceleration of a game object? This wouldn't be possible with the problem I'm encountering.
-
Moving Objects in small incrementsHi, I got a ball hitting a paddle and I want it's X velocity to increase/decrease depending on where it hits the paddle. The code below works with whole numbers but not for numbers like "0.67" or "2.45". I'm looking for a way to animate objects moving at smaller increments for better precision. Thanks.
int speed=Ball_List[b]->speed;
// Paddle center.
int paddle_center = Player->Location.X + Player->Width / 2;// Ball center.
int ball_center = Ball_List[b]->gameBall->Location.X + Ball_List[b]->gameBall->Width / 2;// Find the location on the paddle that the ball hit
int paddle_location = ball_center - paddle_center;// change angle according to distance from center of paddle.
double a=(90-paddle_location);
double angle = (Math::PI / 180) * a; // angle in radians.
Ball_List[b]->xVel = speed*Math::Cos(angle);
Ball_List[b]->yVel = -speed*Math::Sin(angle);// Move the ball
Ball_List[b]->gameBall->Location = Point(Ball_List[b]->gameBall->Location.X + Ball_List[b]->xVel, Ball_List[b]->gameBall->Location.Y + Ball_List[b]->yVel);
[/code] -
Equation for a straight lineThanks for the replies I finally got it working.
-
Equation for a straight lineThanks for the reply. I still run into the problem in my first post. The horizontal movement finishes before the vertical. I ran into this equation for a straight line. y = m*x + b but the speed of the box varies depending on the slope. Can't seem to fix this.
-
Equation for a straight lineHi, I'm trying to animate a box moving from point A to point B. I'm using a .Net timer to animate but I'm not sure on the math to get the box moving in a straight line. So far the box finishes moving down before it finishes moving to the right. Point A x=9,y=6 Point B x=291,y=178 I'm not very good at math but I'm thinking the equation should be straight foreward? Thanks in advance.
-
Anglesso there's no solution then?
-
AnglesI am using doubles in the function. The problem is I can't move the panel(ball) anything less then 1 pixel. Maybe move the ball 1pixel over a couple of frames? I'm not sure how to do that. Here is the code.
int speed=Ball\_List\[b\]->speed; // Paddle center. int paddle\_center = Player->Location.X + Player->Width / 2; // Ball center. int ball\_center = Ball\_List\[b\]->gameBall->Location.X + Ball\_List\[b\]->gameBall->Width / 2; // Find the location on the paddle that the ball hit int paddle\_location = ball\_center - paddle\_center; // Increase X speed according to distance from center of paddle. double a=(90-paddle\_location); double angle = (Math::PI / 180) \* a; // angle in radians. Ball\_List\[b\]->xVel = speed\*Math::Cos(angle); Ball\_List\[b\]->yVel = -speed\*Math::Sin(angle); Ball\_List\[b\]->gameBall->Top = Player->Location.Y - 16;
Thanks
-
AnglesThanks for the replies. Both problems are mostly solved. I have the paddle/ball equation figured out but I'm having a problem where any value less then 1 is ignored... any ideas? I need finer precision. Thanks.
-
AnglesI got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.
double a = 135;
double angle = (Math::PI / 180) * a;
Ball_List[b]->xVel = speed*Math::Cos(angle);
Ball_List[b]->yVel = -speed*Math::Sin(angle);Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.
-
Timer() - anything faster?I'm testing the collisions with a brick with pixel accuracy. It's the only way I could get the collision working properly.
-
Timer() - anything faster?I am animating a ball and I need something faster then what I'm getting with the form Timer() control. Is there anything faster that I can use to speed up the animation? Thanks.
-
Ball PhysicsThis is as close as I got so far but it makes the ball pause for a moment when it hits the top and bottom of the brick. This code is in the timer event. Any solution to this? Thanks.
// Check for brick collisions.
if(left == false && Ball_List[b]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
{if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; left = true; top = true; } } else {left=false;} if(top == false && Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; top = true; } } else{top=false;}
-
Ball PhysicsI tried using && instead of || but it messed up the ball physics. The ball would return in the same direction back towards the paddle. The problem seems to be that Ball.right is is always going to be greater then Ball.left when the ball is colliding with the top or bottom of the brick so the check for horizontal collision will also get executed. I'm not sure how to work around this. Thanks.
-
Ball PhysicsHi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.
if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; } if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; } }
-
error messageThanks for the suggestion. It helped.
-
error messageHi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.
#include "stdafx.h"
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;public ref class Ball
{
public:
PictureBox^ gameBall;
int speed;
double xVel;
double yVel;Ball() // Class constructor. { } void Add\_to\_form( Form ^ form ) { // Variables speed = 5; xVel = speed; yVel = speed; gameBall = gcnew PictureBox(); gameBall->BackColor = Color::LimeGreen; gameBall->Size = Drawing::Size(20,20); gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50); form->Controls->Add(gameBall); }
};
public ref class Form1 : public Form
{
public:
PictureBox^ paddlePlayer;
Timer^ gameTimer;
Ball ball1; // Create ball1.Form1() // Class constructor. { this->Size = Drawing::Size(640,480); this->BackColor= Color::SkyBlue; this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove); gameTimer = gcnew Timer(); gameTimer->Interval = 20; gameTimer->Start(); gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick); paddlePlayer = gcnew PictureBox(); paddlePlayer->BackColor = Color::LimeGreen; paddlePlayer->Size = Drawing::Size(128,16); paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65); this->Controls->Add(paddlePlayer); this->Controls->Add(ball1.gameBall); } System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e) { if(e->X < this->Width-paddlePlayer->Width - 18) { paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y); } } System::Void gameTimer\_Tick(System::Object^ sender, System::EventArgs^ e) { //Move the game ball. ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); // Check for t
-
Search AlgorithmThanks for the advice. I am a beginer coder and have lots of learning to do. :)
-
Search AlgorithmThanks for taking the time to reply and for the advice. I agree my coding is a really messy and needs a re-write. :(