質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

Q&A

1回答

924閲覧

当たり判定 反射するようにするには

rsetuhbcln

総合スコア13

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

0グッド

0クリップ

投稿2017/12/05 05:12

現在、上手くいかないことはボールがカゴに衝突した後の処理です。
カゴにあたった後、ボールが反射するにはどうすればいいですか?
if文の処理にコードを描いたものの、上手く反射することができませんでした
解決法がわかる方教えてください。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace _1108_ボール_斜方投射
{
public partial class Formballshoot : Form
{

double ballPos_x; double ballPos_y; double vx = 10;      //x座標の初速度 double vy = -30;       //y座標の初速度 double g = 1.0;       //加速度 double power = 1; int t = 0; //時間 int ballRadius = 10;     //ボールの半径 double deg; //打ちだし角度 Timer timer = new Timer(); Random rand = new Random(); //乱数を発生させるrandを生成 public Formballshoot() { //角度を30度~60度の間で発生 deg = rand.Next(30, 60); this.ballPos_x = 10; //x座標の初期位置 this.ballPos_y = 650; //y座標の初期位置 InitializeComponent(); PictureBox1.Left = 460; //カゴのy座標の初期位置 PictureBox1.Top = 360; //カゴのx座標の初期位置 //マウスホイールイベントの追加 this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.Scroll_MouseWheel); //タイマーイベント Timer timer = new Timer(); timer.Interval = 50; timer.Tick += new EventHandler(Update); //timer.Start(); } private void Update(object sender, EventArgs e) { ballPos_x += vx * Math.Cos(deg * (Math.PI / 180)); ballPos_y += vy * Math.Sin(deg * (Math.PI / 180)) + g * t; this.t++; //再描写 Invalidate(); //ボールが画面の下を超えた場合 //繰り返しボールを飛ばす if (ballPos_y > 650) { this.vx = power; //初速をpowerにする this.vy = -30;      //y座標の初速度を再び示す  this.g = 1.0; //加速度を再び示す this.ballPos_x = 10; //x座標の初期位置に戻す this.ballPos_y = 650; //y座標の初期位置に戻す deg = rand.Next(30, 60); //角度を30度~60度に設定 this.t = 0; this.t++; ballPos_x += vx * Math.Cos(deg * (Math.PI / 180)); ballPos_y += vy * Math.Sin(deg * (Math.PI / 180)) + g * t; } //ボールとカゴの当たり判定 if ((ballPos_x > PictureBox1.Location.X) && (ballPos_x < PictureBox1.Left + PictureBox1.Size.Width) && (ballPos_y > PictureBox1.Location.Y) && (ballPos_y < PictureBox1.Top + PictureBox1.Size.Height)) { } } private void DrawFiguare(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //円を描画する SolidBrush brush = new SolidBrush(Color.Red); float px = (float)this.ballPos_x - this.ballRadius; float py = (float)this.ballPos_y - this.ballRadius; e.Graphics.FillEllipse(brush, px, py, this.ballRadius * 2, this.ballRadius * 2); } private void endEToolStripMenuItem_Click(object sender, EventArgs e) { //ゲーム終了 Close(); } private void Scroll_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { int Wheel = (e.Delta / 120); label1.Text = (e.Delta * SystemInformation.MouseWheelScrollLines / 120).ToString(); //ホイールを上方向に動かす場合 if (e.Delta > 0) { this.power++;    //初速を上げる } //ホイールを下方向に動かす場合 else if (e.Delta < 0) { this.power--; //初速を下げる } } private void Formballshoot_KeyDown(object sender, KeyEventArgs e) { Timer timer = new Timer(); timer.Interval = 50; //エンターキーを押した場合 if (e.KeyData == Keys.Enter) { timer.Tick += new EventHandler(Update); timer.Start(); //タイマースタート } } }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

完全弾性衝突と仮定して

衝突面の法線ベクトル方向にボールの速度ベクトルを射影して
その2倍を元の速度ベクトルから引けば反射後の速度ベクトルが求まります。

...WinFormsやめてUnityで開発した方がいいのでは?

投稿2017/12/05 05:27

ozwk

総合スコア13521

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rsetuhbcln

2017/12/05 07:26

unityのほうがやはりいいですかね ここまできたのでwinFormでやろうと思います
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問